我是iOS编程新手。我正在尝试在我的应用程序中验证用户。我在android中完成了这个,没有使用JSON的任何问题。我想在这里做同样的事情。我看过this并将代码应用到我的应用中,但我收到此错误:
There was an error processing the request
以下是我从网址检索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;
}
我不知道错误在哪里。我也会发布我的安卓代码[因为在这里我想和我做的完全一样]
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;
}
答案 0 :(得分:0)
我认为“处理请求时出错”可能是由.NET服务器发送的?如果我错了,请原谅我。
这会让我相信请求存在问题。
您的问题中没有足够的信息可以准确地告诉您哪些是错的,但这是我将如何解决它。
使用您正在运行的Android应用中的所有HTTP标头记录完整请求。然后从你的非工作iOS应用程序做同样的事情。比较这两个请求,你会看到一个区别。消除差异,它将起作用。
对于iOS,我会使用AFNetworking(https://github.com/AFNetworking/AFNetworking)和https://github.com/AFNetworking/AFNetworkActivityLogger来获取标头信息。使用该库可能会解决问题,因为它将为您完成大部分JSON转换。
解决问题的最快方法是比较工作和非工作请求。