0

I have a block of code, posting data via HttpPost/HttpCLient. Everything works fine while internet is connected. But when the internet is disconnected or even when something goes wrong on server side, there is no way to handle exception.

I have used all exceptions and catch catch(Exception e) catch (ClientProtocolException e)

but unfortunately none of them worked.

heres the code

@Override
protected String doInBackground(String... params) {     
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(_URL);

    String responseString = null;

    try {


        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
        nameValuePairs.add(new BasicNameValuePair("data", this._Data));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));


        Log.w("HTTPRequest",  this._Data );

        HttpResponse response = httpclient.execute(httppost);
        responseString = new BasicResponseHandler().handleResponse(response);

        Log.w("HTTPResponse", responseString);

    } catch (ClientProtocolException e) {

    } catch (IOException e) {

    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } 

    return responseString;

}

Here is the error "Attempt to invoke virtual method 'org.json.JSONObject org.json.JSONArray.getJSONObject(int)' on a null object reference"

devsep
  • 11
  • 1
  • You don't invoke `getJSONObject` in that code, so the exception is not happening here directly. If it is happening in this method, it is happening in the constructors of `DefaultHttpClient` or `HttpPost`; otherwise a `NullPointerException` would be caught by the `catch Exception`. – Andy Turner Aug 01 '16 at 17:27
  • if I remove this line everything works fine: responseString = new BasicResponseHandler().handleResponse(response); – devsep Aug 02 '16 at 05:07

0 Answers0