3

I am relatively new to Android and I am using JSON to get data from a server. On the first loop at line 22, the StringBuilder contains, 500 Internal Server Error and then the jArray ends up coming back null. How can I handle this error?

public static JSONObject getJSON() {
    String jsonString = "";
    InputStream inStream = null;

    //http post
    JSONObject jArray = null;
    try {
        HttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httpPost = new HttpPost(WS_URL);
        httpPost.setHeader("Content-type", "application/json");

        HttpResponse response = httpClient.execute(httpPost);
        HttpEntity entity = response.getEntity();
        inStream = entity.getContent();

        BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();

        String line = null;

        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        inStream.close();
        jsonString = sb.toString();

        jArray = new JSONObject(jsonString);


        //outputTransactions(jArray);

    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

    return jArray;
}
Jacob Malachowski
  • 911
  • 1
  • 9
  • 18

5 Answers5

2

Though its a late reply but it might help others. You need to check the response status from the server before parsing it as JSON.

For ex.

int status_code=response.getStatusLine().getStatusCode();

if(status_code!=200){
    Log.d("MYLOG","ERROR! Response status is"+status_code);
  }
else{
    inStream = entity.getContent();
    BufferedReader reader = new BufferedReader(new InputStreamReader(inStream, "UTF-8"), 8);
    StringBuilder sb = new StringBuilder();

    String line = null;

    while ((line = reader.readLine()) != null) {
        sb.append(line + "\n");
    }
    inStream.close();


   // Rest of your code......
    }

or Optionally you can check the status code and display the error to the user

Like:

else if(status_code==404){
 Log.d("MYLOG","Sorry! Page not found! Check the URL ");

}else if(status_code==500){
 Log.d("MYLOG","Server is not responding! Sorry try again later..");
}

Hope it helps for newbies like you :-)

Rafique Mohammed
  • 3,666
  • 2
  • 38
  • 43
1

A "500 Internal Server" error means the server had a problem responding to your request. You are not getting a JSON string response.

Then when you try to create your jArray, the string is not valid JSON and the JSONObject cannot parse it - it returns "null" as you say.

You can parse the server response to see if it contains this string, and then create whatever jArray object you want, but you can't get a JSON object from a non-JSON string.

Jim
  • 10,172
  • 1
  • 27
  • 36
1

Take a look at this answer: https://stackoverflow.com/a/8148785/1974614

You should check the statusCode against 500

Community
  • 1
  • 1
azurh
  • 410
  • 4
  • 12
0

You should consider use a library to handle the REST requests like: http://square.github.io/retrofit/

If you use a library like that you can get an object from json when a success response is available and other when an error occur.

    MyApi mylogin = restAdapter.create(MyApi.class); //this is how retrofit create your api
    mylogin.login(username,password,new Callback<String>() {
        @Override
        public void success(String s, Response response) {
            //process your response if login successfull you can call Intent and launch your main activity

        }

        @Override
        public void failure(RetrofitError retrofitError) {
            retrofitError.printStackTrace(); //to see if you have errors
        }
    });
    }
Sandro Machado
  • 9,921
  • 4
  • 36
  • 57
0

I got the same problem like you and I solved it because I missed a part while adding GSON .jar files to adding my serverside project. I think you should carrefully add external libraries to your project too like me.With these links I could aware of problem .

LINK 1

LINK 2

Community
  • 1
  • 1
user3751548
  • 115
  • 11