当前位置: 代码迷 >> Iphone >> 关于json 为什么服务端提示小弟我访问了两次
  详细解决方案

关于json 为什么服务端提示小弟我访问了两次

热度:93   发布时间:2016-04-25 06:31:46.0
关于json 为什么服务端提示我访问了两次
为啥在服务器端提示我访问了两次(在//1 和//2)呢 我只想做一次数据的交互 请问是什么地方的问题

基本是这个过程  

当点击categorybutton的时候 将name等信息post到服务端  

然后跳转到CategoryTableView中 在跳转之前在从服务端获得 一个json 并且解析 最后显示在那个table中

-(IBAction)categoryButton:(id)sender{
  //post提交
  NSString *urlString=@"http://192.168.1.6:8080/iphone/jsonTest.action";
  NSString *bodyString=[NSString stringWithFormat:@"name=%@&clsId=%@&sex=%@",username.text,clsId.text,sex.text];
  [appDelegate.kuaidiParams objectForKey:@"value"],self.searchBar.text];
  NSMutableURLRequest *theRequest=[NSMutableURLRequest requestWithURL:[NSURL URLWithString:urlString]];
  [theRequest setHTTPBody:[bodyString dataUsingEncoding:NSUTF8StringEncoding]];
  [theRequest setHTTPMethod:@"POST"];
  [theRequest addValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
  NSURLResponse *response;
  NSError *error;
  NSData *resultData=[NSURLConnection sendSynchronousRequest:theRequest returningResponse:&response error:&error];//1
  NSXMLParser *xmlParser=[[[NSXMLParser alloc] initWithData:resultData] autorelease];
  [xmlParser parse];

  category = [[CategoryTableViewController alloc]
  initWithNibName:@"CategoryTableViewController" bundle:nil];
   
  Growsoft_itemAppDelegate *delegate = [[UIApplication sharedApplication]delegate];
  category.title = @"商品类别";
  category.myData = [[MyDataSource fetchLibraryInformation]retain];
   
  [delegate.navController pushViewController:category animated:YES];
}



@implementation MyDataSource
+(NSDictionary *) fetchLibraryInformation{
  NSString *urlString = [NSString stringWithFormat:@"http://192.168.1.6:8080/iphone/jsonTest.action"];  
  NSURL *url = [NSURL URLWithString:urlString];
  NSLog(@"fetching library data");
  NSString *aa= [self fetchJSONValueForURL:url];
  NSLog(@"%@<--url",aa);
  return aa;
}
+(id)fetchJSONValueForURL:(NSURL *)url{
  NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url
  encoding:NSUTF8StringEncoding error:nil];//2
  id jsonValue = [jsonString JSONValue];
  [jsonString release];
  NSLog(@"%@<--JSONValue",jsonValue);
  return jsonValue;
   
}

@end

------解决方案--------------------
打断点跟一下
------解决方案--------------------
fetchLibraryInformation 这个文件中为什么又建立一个 request??
------解决方案--------------------
你的程序本来就获取了两遍,一遍是通过NSMutableURLRequest获取的,另一遍是在NSString *jsonString = [[NSString alloc] initWithContentsOfURL:url获取的。
  相关解决方案