0

I am having what should be a simple problem

In my activity I make a async function call

json = jsonParser.post(URL, params);

my class + function looks like this (partly based on this answer by kuester2000)

public class json {

static InputStream stream = null;
static JSONObject jObj = null;
static String json = "";

public json() {
    // TODO Auto-generated constructor stub
}

public JSONObject post(String url, List<NameValuePair> NameValuePairs) {
    try {

        String paramString = URLEncodedUtils
                .format(NameValuePairs, "utf-8");

        Log.d("AsyncTask","1:Startet http");
        
        HttpParams httpParameters = new BasicHttpParams();
        // Set the timeout in milliseconds until a connection is established.
        // The default value is zero, that means the timeout is not used. 
        int timeoutConnection = 3000;
        HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
        // Set the default socket timeout (SO_TIMEOUT) 
        // in milliseconds which is the timeout for waiting for data.
        int timeoutSocket = 5000;
        HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);
        
        DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
        HttpPost httpPost = new HttpPost(url);
        Log.d("AsyncTask","1: http Post");
        httpPost.setEntity(new UrlEncodedFormEntity(NameValuePairs));
        Log.d("AsyncTask","1: send http.post");
        HttpResponse httpResponse = httpClient.execute(httpPost);
        Log.d("AsyncTask", " receive Response");
        
        // make sure response is good (Int 200)
        if(httpResponse.getStatusLine().getStatusCode() == 200){
            
        HttpEntity httpEntity = httpResponse.getEntity();
        if (httpEntity != null) {
        Log.d("AsyncTask", " get Entity");
        stream = httpEntity.getContent();

        json = convertStreamToString(stream);
        Log.d("AsyncTask", "Convert Json");
        stream.close();
        } else {
            Log.d("AsyncTask","1: no internet connection");
        }
        Log.d("AsyncTask", "After Convert Json");
        } else {
            Log.e("AsyncTask: ", "server not found error code: " + String.valueOf(httpResponse.getStatusLine().getStatusCode()));
        }

    } catch (UnsupportedEncodingException e) {
        Log.d("AsyncTask","2: no internet connection");
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        Log.d("AsyncTask","3: no internet connection");
        e.printStackTrace();
    } catch (IOException e) {
        Log.d("AsyncTask","7: no internet connection");
        e.printStackTrace();
    }

    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("AsyncTask","error1");
        //Log.e("JSON Parser", "Error parsing data " + e.toString());
    }
    Log.e("AsyncTask","retun to getgame");
    //Log.d("JSON_POST", jObj.toString());
    // return JSON String
    return jObj; // <--- this is the problem
}

My problem is when the server is not responding with an ok, my function is returning null.(making the app terminate) I would somehow like to catch this and return to my async where I want to post a toast with the error code... so that the User knows to try again later.

can anyone help me on how to do this?

Ryan M
  • 18,333
  • 31
  • 67
  • 74
rasmus
  • 125
  • 2
  • 3
  • 11
  • "my function is returning null.(making the app terminate)" : So basically, just check to see if the return is null or not. Either that or create your own JSON string with a 'status' code such as `{"status" : "Error"}` then check for that. – Squonk Aug 10 '14 at 23:18

1 Answers1

0

Instead of returning jObj blindly, you can try to check whether it's null, and if it is, assign it a new blank JSONObject. Replace that last line with:

if (jObj != null) {
    return jObj;
} else {
    return new JSONObject();
}

That should at least remove the null error.

metatoaster
  • 17,419
  • 5
  • 55
  • 66
someguy234
  • 559
  • 4
  • 17
  • 1
    I ended up using this and adding a Json object with error:1 to detect the propblem in my activity. Many thanks – rasmus Aug 11 '14 at 09:20