I am trying some code for accessing a php page which I got from http://www.androidhive.info/2012/05/how-to-connect-android-with-php-mysql/
Specifically, makeHttpRequest in the JSONParser class will work fine if it is able to reach the page specified by url, but crashes otherwise. That method is copied below, but the entire file is as shown on the site linked above:
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
// Making HTTP request
try {
// check for request method
if(method == "POST"){
// request method is POST
// defaultHttpClient
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();
}else if(method == "GET"){
// request method is 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();
}
try {
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 parse the string to a JSON object
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
When debugging I get errors from the first try (connection refused), and then second try (buffer error), and also the last try (JSONParser error). Finally it exits returning jObj (which is null) and then crashes. After stepping past 'return jObj;', it ends up in FutureTask in AsyncTask.java and then UncaughtHandler in RuntimeInit.java.
I assume the problem is that it returns null. How can this be fixed to avoid crashing if the server is not available?
The logcat output is below:
03-18 10:14:07.656 25240-25240/com.example.test I/art? Late-enabling -Xcheck:jni
03-18 10:14:07.746 25240-25240/com.example.test I/System.out? JSONParser constructor
03-18 10:14:08.021 25240-25256/com.example.test I/System.out? url:http://192.168.0.114/get_details.phpmethod:GET
03-18 10:14:08.031 25240-25257/com.example.test D/OpenGLRenderer? Render dirty regions requested: true
03-18 10:14:08.038 25240-25240/com.example.test D/Atlas? Validating map...
03-18 10:14:08.087 25240-25257/com.example.test I/OpenGLRenderer? Initialized EGL, version 1.4
03-18 10:14:08.106 25240-25257/com.example.test D/OpenGLRenderer? Enabling debug mode 0
03-18 10:14:08.110 25240-25257/com.example.test D/mali_winsys? new_window_surface returns 0x3000
03-18 10:14:11.058 25240-25256/com.example.test W/System.err? org.apache.http.conn.HttpHostConnectException: Connection to http://192.168.0.114 refused
03-18 10:14:11.062 25240-25256/com.example.test W/System.err? at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:183)
03-18 10:14:11.062 25240-25256/com.example.test W/System.err? at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
03-18 10:14:11.062 25240-25256/com.example.test W/System.err? at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
03-18 10:14:11.062 25240-25256/com.example.test W/System.err? at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
03-18 10:14:11.063 25240-25256/com.example.test W/System.err? at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
03-18 10:14:11.063 25240-25256/com.example.test W/System.err? at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
03-18 10:14:11.063 25240-25256/com.example.test W/System.err? at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
03-18 10:14:11.063 25240-25256/com.example.test W/System.err? at com.example.test.JSONParser.makeHttpRequest(JSONParser.java:65)
03-18 10:14:11.064 25240-25256/com.example.test W/System.err? at com.example.test.MainActivity$GetDetails.doInBackground(MainActivity.java:430)
03-18 10:14:11.064 25240-25256/com.example.test W/System.err? at com.example.test.MainActivity$GetDetails.doInBackground(MainActivity.java:424)
03-18 10:14:11.064 25240-25256/com.example.test W/System.err? at android.os.AsyncTask$2.call(AsyncTask.java:288)
03-18 10:14:11.064 25240-25256/com.example.test W/System.err? at java.util.concurrent.FutureTask.run(FutureTask.java:237)
03-18 10:14:11.065 25240-25256/com.example.test W/System.err? at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
03-18 10:14:11.065 25240-25256/com.example.test W/System.err? at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
03-18 10:14:11.065 25240-25256/com.example.test W/System.err? at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
03-18 10:14:11.065 25240-25256/com.example.test W/System.err? at java.lang.Thread.run(Thread.java:818)
03-18 10:14:11.065 25240-25256/com.example.test W/System.err? Caused by: java.net.ConnectException: failed to connect to /192.168.0.114 (port 80): connect failed: EHOSTUNREACH (No route to host)
03-18 10:14:11.068 25240-25256/com.example.test W/System.err? at libcore.io.IoBridge.connect(IoBridge.java:124)
03-18 10:14:11.069 25240-25256/com.example.test W/System.err? at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183)
03-18 10:14:11.069 25240-25256/com.example.test W/System.err? at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:456)
03-18 10:14:11.069 25240-25256/com.example.test W/System.err? at java.net.Socket.connect(Socket.java:882)
03-18 10:14:11.070 25240-25256/com.example.test W/System.err? at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119)
03-18 10:14:11.070 25240-25256/com.example.test W/System.err? at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:144)
03-18 10:14:11.071 25240-25256/com.example.test W/System.err? ... 15 more
03-18 10:14:11.072 25240-25256/com.example.test W/System.err? Caused by: android.system.ErrnoException: connect failed: EHOSTUNREACH (No route to host)
03-18 10:14:11.073 25240-25256/com.example.test W/System.err? at libcore.io.Posix.connect(Native Method)
03-18 10:14:11.078 25240-25256/com.example.test W/System.err? at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:111)
03-18 10:14:11.078 25240-25256/com.example.test W/System.err? at libcore.io.IoBridge.connectErrno(IoBridge.java:137)
03-18 10:14:11.078 25240-25256/com.example.test W/System.err? at libcore.io.IoBridge.connect(IoBridge.java:122)
03-18 10:14:11.082 25240-25256/com.example.test W/System.err? ... 20 more
03-18 10:14:11.083 25240-25256/com.example.test E/Buffer Error? Error converting result java.lang.NullPointerException: lock == null
03-18 10:14:11.083 25240-25256/com.example.test E/JSON Parser? Error parsing data org.json.JSONException: End of input at character 0 of
03-18 10:14:11.090 25240-25256/com.example.test E/AndroidRuntime? FATAL EXCEPTION: AsyncTask #1
Process: com.example.test, PID: 25240
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.toString()' on a null object reference
at com.example.test.MainActivity$GetDetails.doInBackground(MainActivity.java:433)
at com.example.test.MainActivity$GetDetails.doInBackground(MainActivity.java:424)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
0 comments:
Post a Comment