0

I am new to iOS programming. I am trying to authenticate a user in my application. I have done this in android without any problem using JSON. I want to do the same thing here. I have seen this and apply the code with my app, but I get this error:

There was an error processing the request

Below is my entire code for retrieving the JSON from the url:

-(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;
}

I don't know where the error is. I will post my android code also [because here i want exactly the same thing as i did ]

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;
}
Community
  • 1
  • 1
Darshana
  • 314
  • 2
  • 4
  • 22

1 Answers1

0

I think "There was an error processing the request" might be sent by the .NET server? Forgive me if I am wrong.

That would lead me to believe there is a problem with the request.

There is not enough information in your question to tell you precisely what is wrong, but here is how I would solve it.

Log the complete request with all HTTP headers from your working android app. Then do the same from your non-working iOS app. Compare the two requests and you will see a difference. Eliminate the difference and it will work.

For iOS I would use AFNetworking (https://github.com/AFNetworking/AFNetworking) and https://github.com/AFNetworking/AFNetworkActivityLogger to get the header information. Using the library might solve the problem as it will do much of that JSON conversion for you.

The fastest way to solve your problem is going to be to compare the working and non working requests.

Vincil Bishop
  • 1,594
  • 17
  • 21