【发布时间】: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