【问题标题】:how to GET responses when data post to URL数据发布到 URL 时如何获取响应
【发布时间】:2014-07-31 10:48:36
【问题描述】:

我知道如何使用 POST 将数据(UITextField 值)发布到 JSON url。但现在我尝试使用 GET 方法将数据发布到服务器。我有 10 个文本字段。我尝试过这样的方式

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);
    }
 }

提交后控制台将显示此消息“URL3 出现问题” 我知道发布,但现在我还需要发布数据和获取响应。所以我使用了 GET 方法,但没有得到响应。所以请给我任何想法。然后请告诉我我的代码有什么问题。

【问题讨论】:

  • 使用 POST Method Man,并检查你的字符串是否与服务器字符串匹配

标签: ios objective-c json post get


【解决方案1】:

使用回调函数

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error

如果调用失败,错误变量将帮助您了解问题所在,以便您自己解决。

将请求的内容类型字段设置为 application/json 可能也有帮助。

【讨论】:

  • 感谢您的回复,我还添加了该方法 -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error { if (connection ==con3) { NSLog(@"SOMETHING WENT WRONG与 URL3"); } } 提交按钮后控制台将显示“URL3 出现问题”消息
  • 在方法的实现中将 ns 日志更改为 NSLog(@"Error: %@", error.description"); 仅仅打印它失败是没有意义的,试着看看它失败的实际原因通过查看 NSError。它可能能够告诉您出了什么问题。或者,在调试模式下运行代码并以这种方式查看变量。
  • thanks when place line NSLog(@"Error: %@", error.description"); 显示如下 Domain=kCFErrorDomainCFNetwork Code=303 "操作无法完成。 (kCFErrorDomainCFNetwork 错误 303。)“UserInfo=0x13786c60 {NSErrorFailingURLKey=192.168.3.125:8090/SaveDollar/rest/deals/add, NSErrorFailingURLStringKey=192.168.3.125:8090/SaveDollar/rest/deals/add}
【解决方案2】:

您应该像这样收集响应数据:

- (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;

【讨论】:

  • 感谢我在这里放置的回复 if(con3) { webData3=[NSMutableData data]; NSLog(@"连接成功"); NSLog(@"GOOD Day 我的数据%@",webData3); }
  • 我放在这里 - (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]; } }
  • 答案不是实际的,因为问题已经改变了
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-31
  • 1970-01-01
  • 2010-11-04
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多