2014-11-25 3 views
0

HTTP 요청 postget 응답을 간단한 목표 C 코드 방법을 사용하여 생성하려고합니다. 아래 코드는 내가 성공적으로 게시 할 수 있습니다. 하지만 응답 데이터를받지 못했습니다. 응답 인쇄 null 값만. 제발 도와주세요, 응답 데이터를 받아야합니다. 내 코드를 매우 간단NULL 값을 표시하는 HTTP 요청 GET 응답

NSString *post = [NSString stringWithFormat:@"name=%@",name]; 
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; 
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:URLPATH]]]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:postLength forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:postData]; 
NSURLConnection *conn = [[NSURLConnection alloc]initWithRequest:request delegate:self]; 

if(conn) { 
     NSLog(@"Connection Successful"); 
} else { 
     NSLog(@"Connection could not be made"); 
} 



// Create GET method 
NSError *err; 
NSURLResponse *responser; 
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&responser error:&err]; 

// JSON Formatter 
NSError *error; 
NSDictionary *jsonsDictionary = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:&error]; 
NSDictionary *respon = [jsonsDictionary objectForKey:@"response"]; 
NSLog(@"UPDATE RESPONSE : %@", respon); 
+0

응답이 유효 JSON되지 않습니다 : 그래서이 경우 당신은 데이터를 문자열로 변환하고 컨텐츠 유형 헤더를 설정 "application/x-www-form-urlencoded" 또는 사용 다음 코드로 요청 헤더 "Content-Type"을 설정할 필요가 없다. responseData에서 NSString을 생성하고이를 출력하십시오. 또한 오류 및 오류 변수를 인쇄하십시오. –

+0

일부 코드는 게시 할 수 있습니까? 나는 너를 못 잡는다. – Mano

+0

NSLog (@ "% @", err); 및 NSLog (@ "% @", 오류); –

답변

0

그 아래에 다음

:

//-- Convert string into URL 
    NSString *jsonUrlString = [NSString stringWithFormat:@"demo.com/your_server_db_name/service/link"]; 
    NSURL *url = [NSURL URLWithString:[jsonUrlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]]; 

    //-- Send request to server 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url]; 
    //-- Send username & password for url authorization 
    [request setValue: jsonUrlString forHTTPHeaderField:@"Content-Length"]; 
    [request setHTTPMethod:@"POST"]; //-- Request method GET/POST 

    //-- Receive response from server 
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; 

    //-- JSON Parsing with response data 
    NSDictionary *result = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingMutableContainers error:nil]; 
    NSLog(@"Result = %@",result); 

샘플 : https://github.com/svmrajesh/Json-Sample

+1

JSON을이 사이트의 Objective-C 컬렉션으로 변환하는 방법에 대한 예제가 이미 수백 가지가있는 이유는 무엇입니까? – trojanfoe

+0

authValue 란 무엇입니까? – Mano

+0

이 코드를 사용하지 마십시오. 버그가 많이 있습니다. 코드에는 버그가 없으므로 서버가 응답하는 데이터를 찾아야합니다. 잘못된 응답이 반환됩니다. –

0

당신의 데이터 게시물 NSString *post = [NSString stringWithFormat:@"name=%@",name];입니다.

NSData *postData = [post dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES]; 
... 
[request setValue:@"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
+0

요청 헤더가 필요하지 않을 수 있습니다. 그것은 서버가 기대하는 것에 따라 다르며, 일부는 Content-Type 헤더를 무시할 것입니다. 그는 서버에서 응답을 받고 있으며 코드가이를 처리하도록해야합니다. –

+0

이 경우 필요하지 않다고 생각합니다. 본문 데이터 형식 정보 내용 – larva

+0

유용한 정보를 게시하십시오. – Mano