Reputation: 167
I was trying to run my project when there is no network connection.If network is available no problem is occured.
I got this error,
01-24 17:41:19.725: E/AndroidRuntime(1077): FATAL EXCEPTION: AsyncTask #3
01-24 17:41:19.725: E/AndroidRuntime(1077): java.lang.RuntimeException: An error occured while executing doInBackground()
01-24 17:41:19.725: E/AndroidRuntime(1077): at android.os.AsyncTask$3.done(AsyncTask.java:299)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
01-24 17:41:19.725: E/AndroidRuntime(1077): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
01-24 17:41:19.725: E/AndroidRuntime(1077): at java.lang.Thread.run(Thread.java:856)
01-24 17:41:19.725: E/AndroidRuntime(1077): Caused by: java.lang.NullPointerException
01-24 17:41:19.725: E/AndroidRuntime(1077): at org.json.JSONTokener.nextCleanInternal(JSONTokener.java:116)
01-24 17:41:19.725: E/AndroidRuntime(1077): at org.json.JSONTokener.nextValue(JSONTokener.java:94)
01-24 17:41:19.725: E/AndroidRuntime(1077): at org.json.JSONObject.<init>(JSONObject.java:154)
01-24 17:41:19.725: E/AndroidRuntime(1077): at org.json.JSONObject.<init>(JSONObject.java:171)
01-24 17:41:19.725: E/AndroidRuntime(1077): at com.example.sms.JsonParser.makeHttpRequest(JsonParser.java:136)
01-24 17:41:19.725: E/AndroidRuntime(1077): at co m.example.sms.LoginActivity$AttemptLogin.doInBackground(LoginActivity.java:298)
01-24 17:41:19.725: E/AndroidRuntime(1077): at com.example.sms.LoginActivity$AttemptLogin.doInBackground(LoginActivity.java:1)
01-24 17:41:19.725: E/AndroidRuntime(1077): at
This is my JsonParser class,
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
try {
json=null;
jsonObject= null;
if(method.equalsIgnoreCase("POST")){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
System.out.println(is);
System.out.println("getting the content");
}else if(method.equalsIgnoreCase("GET")){
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
System.out.println("getting all the values ");
try {
json = null;
jsonObject =null;
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 {
Log.i("tagconvertstr1", "["+json+"]");
//if(TextUtils.isEmpty(json))
if(json!=null||json!="null")
{
jss= "not empty";
System.out.println("json is not equal to null");
jsonObject = new JSONObject(json);
System.out.println("json object parse finished");
Log.i("tagconvertstr2","["+json+"]");
}
else
{
System.out.println(" it is null value ");
jss= "empty";
System.out.println("jss value is" +jss);
}
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
System.out.println("jsson object is" + json);
return jsonObject;
}
I dont know why I got this error even I checked for null!Can anyone help me?
Upvotes: 1
Views: 995
Reputation: 1423
As you said that this error occured when networking is disconnected. It means that you are received NULL value and pass the NULL value to json. JSONObject unable to catch the error if you assign null to constructor. So, to solve this problems replace following code
jsonObject=new JSONObject(json) ;
to
jsonObject=new JSONObject(json == null ? "" : json) ;
Upvotes: 1
Reputation: 3576
Before calling webservice , check internet is available or not.
if(isInternetConnected()){
// call your webservice
}
else{
AlertUtils.showNetworkAlert(RegisterActivity.this);
}
to check net connection.
public static boolean isInternetConnected(){
ConnectivityManager cm = (ConnectivityManager) SSApplication.getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
boolean isConnected = false;
if(info != null && info.isConnectedOrConnecting()){
isConnected = true;
}
return isConnected;
}
Upvotes: 0