【发布时间】:2014-02-21 05:38:30
【问题描述】:
我是 iOS 编程新手。我正在尝试对我的应用程序中的用户进行身份验证。我在 android 中使用 JSON 没有任何问题。我想在这里做同样的事情。我已经看到 this 并将代码应用到我的应用程序中,但我收到此错误:
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;
}
【问题讨论】:
-
请有人帮帮我...
-
[请求 setHTTPBody: __jsonString];而不是 [请求 setHTTPBody: __jsonData];
-
@CoolMonster 我试过但没用.. :(