Aravind M J Aravind M J - 3 years ago 147
PHP Question

App Force Closing on calling doInBackground for getting a JSON file from the server

I want to get a JSON data created on calling a PHP file on the server and store the JSON data to SQLite database in the android app. But when I click the button to execute the fetching process the app force closes.
This is my code sample

else if (type.equals("getjson")){
try {
URL url = new URL(json_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("GET");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput((true));
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result ="";
String line="";
while ((line = bufferedReader.readLine())!=null){
result += line;
}
bufferedReader.close();
inputStream.close();
// return result;
JSONArray jsonArray = null;
try {

jsonArray = new JSONArray(result);
JSONObject jsonObject;

for (int i = 0; i < jsonArray.length(); i++) {

jsonObject = jsonArray.getJSONObject(i);

String tempEid = jsonObject.getString("eid");

String tempEname = jsonObject.getString("ename");

boolean res = db.insertDb(tempEid, tempEname);
return String.valueOf(res);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


This is the error log

FATAL EXCEPTION: AsyncTask #1
Process: com.iamaravind.dhishnareg, PID: 14847
java.lang.RuntimeException: An error occurred while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:325)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)
Caused by: java.lang.ArrayIndexOutOfBoundsException: length=1; index=1
at com.iamaravind.dhishnareg.BackgroundWorker.doInBackground(BackgroundWorker.java:41)
at com.iamaravind.dhishnareg.BackgroundWorker.doInBackground(BackgroundWorker.java:31)
at android.os.AsyncTask$2.call(AsyncTask.java:305)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
at java.lang.Thread.run(Thread.java:761)


Please check whether the code for getting the json is correct.

public void getJson(View view)
{
String type = "getjson";
BackgroundWorker backgroundWorker = new BackgroundWorker(this);
backgroundWorker.execute(type);
}


This is the Calling function.

Answer Source

You should not return from inside the for loop in every condition.

You should remove the line return String.valueOf(res); from inside the for loop and handle error with a different logic. You may return false if res == false, But not unconditionally. Otherwise there would be no use of the loop.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download