我正在向服務器發送數據,它正在響應我的請求,並且在Web中收到如下所示的文本...我的問題在哪裏?NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
它在responseData
中有數據時沒有響應我的NSData。在NSURLConnection中將NSData轉換爲NSString = null
我的響應消息:
{ret:0,msg:"<B>Your request has submitted</B><br/>Your request number is 123 <br/>Have a nice day.<br/>"}
我的代碼:
NSString *link = @"http://example.jsp";
NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"first_name=%@&last_name=%@&phone=%@", link, nameField.text, surnameField.text, phoneField.text]];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
NSURLResponse *response;
NSError *err;
NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
NSLog(@"responseData: %@", responseData);
NSString* newStr = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
NSLog(@"Finish %@", newStr); // newStr is null!!
UIAlertView *message = [[UIAlertView alloc] initWithTitle:title
message:newStr
delegate:self
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[message show];
你可以顯示你的網址結果 – 2014-10-28 12:52:49
@ Anbu.Karthik http://example.com/on/form.jsp?first_name=test1&last_name=test2&phone=34252252345但它不會幫助。 – CAN 2014-10-28 13:00:28
你的網址是無效的,這是你沒有得到答案,但編碼是好的,在你的代碼沒有問題,只是檢查你的網址是否有效 – 2014-10-28 13:02:54