将Json对象传递给IOS中的方法
我不熟悉iOS编程。我正在尝试验证应用程序中的用户。我在android中使用JSON做过这件事,没有任何问题。我想在这里做同样的事情。我已在我的应用程序中看到并应用了该代码,但我遇到以下错误:将Json对象传递给IOS中的方法,ios,json,Ios,Json,我不熟悉iOS编程。我正在尝试验证应用程序中的用户。我在android中使用JSON做过这件事,没有任何问题。我想在这里做同样的事情。我已在我的应用程序中看到并应用了该代码,但我遇到以下错误: There was an error processing the request 下面是我从url检索JSON的完整代码: -(NSString*) getjsonFromURl:(NSURL*)url :(NSArray*) key : (NSArray*)value; { NSDictio
There was an error processing the request
下面是我从url检索JSON的完整代码:
-(NSString*) getjsonFromURl:(NSURL*)url :(NSArray*) key : (NSArray*)value;
{
NSDictionary *jsonDictionary = [NSDictionary dictionaryWithObjects:value forKeys:key];
if([NSJSONSerialization isValidJSONObject:jsonDictionary])
{
__jsonData = [NSJSONSerialization dataWithJSONObject:jsonDictionary options:0 error:nil];
__jsonString = [[NSString alloc]initWithData:__jsonData encoding:NSUTF8StringEncoding];
}
// Be sure to properly escape your url string.
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setHTTPBody: __jsonData];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [__jsonData length]] forHTTPHeaderField:@"Content-Length"];
NSError *errorReturned = nil;
NSURLResponse *theResponse =[[NSURLResponse alloc]init];
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&theResponse error:&errorReturned];
if (errorReturned)
{
NSLog(@"Error %@", errorReturned);
}
else
{
responseString=[NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers|NSJSONReadingAllowFragments error:nil];
NSLog(@"%@", responseString);
}
return responseString;
}
我不知道错误在哪里。我也会发布我的android代码[因为在这里我想要和我一样的东西]
public JSONObject getJSONFromUrl(JSONObject parm,String url) throws JSONException
{
InputStream is = null;
JSONObject jObj = null;
String json = "";
// Making HTTP request
try
{
// defaultHttpClient
/*JSONObject parm = new JSONObject();
parm.put("agencyId", 27);
parm.put("caregiverPersonId", 47);*/
/* if(!(jObj.isNull("d"))){
jObj=null;
}
*/
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.addHeader("Content-Type", "application/json; charset=utf-8");
HttpEntity body = new StringEntity(parm.toString(), "utf8");
httpPost.setEntity(body);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
catch (UnsupportedEncodingException e)
{
e.printStackTrace();
}
catch (ClientProtocolException e)
{
e.printStackTrace();
} catch (IOException e)
{
e.printStackTrace();
}
try
{
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
is.close();
json = sb.toString();
}
catch (Exception e)
{
Log.e("Buffer Error", "Error converting result " + e.toString());
}
// try parse the string to a JSON object
try
{
jObj = new JSONObject(json);
}
catch (JSONException e)
{
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
我认为“处理请求时出错”可能是由.NET服务器发送的?如果我错了,请原谅我
这会让我相信这个请求有问题
在你的问题中没有足够的信息来准确地告诉你什么是错的,但下面是我将如何解决它
使用工作中的android应用程序中的所有HTTP头记录完整请求。然后从你的非工作iOS应用程序中执行同样的操作。比较这两个请求,您将看到不同之处。消除差异,它就会起作用
对于iOS,我将使用AFNetworking()和获取标题信息。使用该库可能会解决这个问题,因为它将为您完成大部分JSON转换
解决问题的最快方法是比较工作请求和非工作请求。请有人帮助我…[请求setHTTPBody:uu jsonString];而不是[请求setHTTPBody:_jsonData]@CoolMonster我试过了,但没有成功(请参阅此