我知道如何使用POST将数据(UITextField值)发布到JSON url。但是现在我尝试使用GET方法将数据发布到server.I有10个textFields.I尝试过这样的方式
NSString *post1 =[NSString stringWithFormat:@"?&dealImage=%@&dealcatid=%@&DeaTitle=%@&DealDesc=%@&price=%@&cityId=%@&StartDate=%@&EndDate=%@&FromTime=%@&ToTime=%@",
strEncoded, string1 ,pTitle.text ,Description.text ,pPrice.text,string2,beginDate,endDate,beginTime,endTime];
NSData *postData = [post1 dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d",[postData length]];
NSLog(@"array array %@",postLength);
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init] ;
[request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://192.168.3.125:8090/SaveDollar/rest/deals/add"]]];
NSLog(@"getData%@",request);
[request setHTTPMethod:@"GET"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Current-Type"];
[request setHTTPBody:postData];
NSLog(@"getData%@",request);
con3 = [[NSURLConnection alloc]initWithRequest:request delegate:self];
if(con3)
{
webData3=[NSMutableData data];
NSLog(@"Connection successfull");
NSLog(@"GOOD Day My data %@",webData3);
}
else
{
NSLog(@"connection could not be made");
}
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
if (connection ==con3)
{
[webData3 setLength:0];
}
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
if (connection ==con3)
{
[webData3 appendData:data];
}
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
if (connection ==con3) {
NSLog(@"SOMETHING WENT WRONG WITH URL3");
}
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection{
if (connection==con3)
{ NSLog(@"Succeeded! Received %d bytes of data",[webData3 length]);
NSLog(@"Data is %@",webData3);
NSString *responseText = [[NSString alloc] initWithData:webData3 encoding: NSASCIIStringEncoding];
NSLog(@"Response: %@", responseText);//holds textfield entered value
NSString *newLineStr = @"\n";
responseText = [responseText stringByReplacingOccurrencesOfString:@"<br />" withString:newLineStr];
NSLog(@"ResponesText %@",responseText);
}
}
在提交控制台之后,将显示此消息&#34; STERETHING WENT WRENT WITH URL3&#34; 我知道发帖但现在我需要发布数据和获取响应。所以我使用GET方法,但我没有得到回应。所以请给我任何想法。然后请告诉我我的代码有什么问题。
答案 0 :(得分:-1)
你应该收集这样的回复数据:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
_responseData = [[NSMutableData alloc] init];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[_responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// connection fail
}
其中数据为NSMutableData *_responseData;
答案 1 :(得分:-1)
使用回调功能
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
如果调用失败,错误变量将帮助您查看出现的问题,以便您自己解决。
可能有用的一件事是将您的请求的内容类型字段设置为application / json。