【问题标题】:passing Json object to a method in IOS将 Json 对象传递给 IOS 中的方法
【发布时间】: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 我试过但没用.. :(

标签: ios json


【解决方案1】:

我认为“处理请求时出错”可能是由 .NET 服务器发送的?如果我错了,请原谅我。

这会让我相信请求有问题。

您的问题中没有足够的信息来准确地告诉您出了什么问题,但这是我将如何解决的方法。

使用您正在运行的 Android 应用程序中的所有 HTTP 标头记录完整的请求。然后从您的非工作 iOS 应用程序中执行相同的操作。比较这两个请求,您会发现不同。消除差异,它就会起作用。

对于 iOS,我会使用 AFNetworking (https://github.com/AFNetworking/AFNetworking) 和 https://github.com/AFNetworking/AFNetworkActivityLogger 来获取标头信息。使用该库可能会解决问题,因为它会为您完成大部分 JSON 转换。

解决问题的最快方法是比较工作请求和非工作请求。

【讨论】:

    猜你喜欢
    • 2020-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-09
    • 2011-09-27
    • 2014-12-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多