Wednesday, April 25, 2018

java - DefaultHttpClient AsyncTask class timeout exceptions

This is my AsyncTask class:




public class JSONParser extends AsyncTask {


public interface MyCallbackInterface {
public void onRequestComplete(JSONObject result);
}

private MyCallbackInterface mCallback;

public JSONParser(MyCallbackInterface callback) {
mCallback = callback;

Context context = (Context) callback;

}

public JSONObject getJSONFromUrl(String url) {

// Making HTTP request
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpPost = new HttpGet(url);
HttpResponse getResponse = null;

final HttpParams httpParameters = httpClient.getParams();

HttpConnectionParams.setConnectionTimeout(httpParameters, 10 * 10000);
HttpConnectionParams.setSoTimeout (httpParameters, 10 * 10000);

try {
getResponse = httpClient.execute(httpPost);
} catch (ClientProtocolException e2) {
e2.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}


final int statusCode = getResponse.getStatusLine().getStatusCode();

if (statusCode != HttpStatus.SC_OK) {
Log.w(getClass().getSimpleName(),
"Error " + statusCode + " for URL " + url);
return null;
}

HttpEntity getResponseEntity = getResponse.getEntity();


String json = null;
try {
json = EntityUtils.toString(getResponseEntity);
} catch (ParseException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}


JSONObject jObj = null;
// 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;

}

@Override
protected JSONObject doInBackground(String... params) {
String url = params[0];
return getJSONFromUrl(url);
}

@Override
protected void onPostExecute(JSONObject result) {


mCallback.onRequestComplete(result);

}

}


I get the JSON answer in UI (in class RegisterActivity extends Activity implements MyCallbackInterface) with:





public class RegisterActivity extends Activity implements MyCallbackInterface{
...
@Override
public void onRequestComplete(JSONObject json) {
//hire parse and display json
...


I call JSONParser from the same UI (class RegisterActivity extends Activity implements MyCallbackInterface)




JSONParser mJSONParser1 = new JSONParser(context111);
mJSONParser1.execute(SERVER_URL+"?method=registration&user="+user+"&pass="+pass+"®Id="+regId);


The exceptions:




10-03 15:58:02.303: W/System.err(22414): java.net.SocketException: Connection timed out
10-03 15:58:02.350: W/System.err(22414): at org.apache.harmony.luni.platform.OSNetworkSystem.read(Native Method)
10-03 15:58:02.366: W/System.err(22414): at dalvik.system.BlockGuard$WrappedNetworkSystem.read(BlockGuard.java:273)

10-03 15:58:02.366: W/System.err(22414): at org.apache.harmony.luni.net.PlainSocketImpl.read(PlainSocketImpl.java:458)
10-03 15:58:02.366: W/System.err(22414): at org.apache.harmony.luni.net.SocketInputStream.read(SocketInputStream.java:85)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:179)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)

10-03 15:58:02.366: W/System.err(22414): at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:425)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-03 15:58:02.366: W/System.err(22414): at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-03 15:58:02.366: W/System.err(22414): at com.androidhive.pushnotifications.JSONParser.getJSONFromUrl(JSONParser.java:72)
10-03 15:58:02.366: W/System.err(22414): at com.androidhive.pushnotifications.JSONParser.doInBackground(JSONParser.java:121)
10-03 15:58:02.366: W/System.err(22414): at com.androidhive.pushnotifications.JSONParser.doInBackground(JSONParser.java:1)
10-03 15:58:02.366: W/System.err(22414): at android.os.AsyncTask$2.call(AsyncTask.java:185)
10-03 15:58:02.366: W/System.err(22414): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)

10-03 15:58:02.366: W/System.err(22414): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-03 15:58:02.366: W/System.err(22414): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-03 15:58:02.374: W/System.err(22414): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-03 15:58:02.374: W/System.err(22414): at java.lang.Thread.run(Thread.java:1019)
10-03 15:58:02.374: W/dalvikvm(22414): threadid=9: thread exiting with uncaught exception (group=0x40018560)
10-03 15:58:02.374: E/AndroidRuntime(22414): FATAL EXCEPTION: AsyncTask #1
10-03 15:58:02.374: E/AndroidRuntime(22414): java.lang.RuntimeException: An error occured while executing doInBackground()
10-03 15:58:02.374: E/AndroidRuntime(22414): at android.os.AsyncTask$3.done(AsyncTask.java:200)
10-03 15:58:02.374: E/AndroidRuntime(22414): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
10-03 15:58:02.374: E/AndroidRuntime(22414): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)

10-03 15:58:02.374: E/AndroidRuntime(22414): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
10-03 15:58:02.374: E/AndroidRuntime(22414): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
10-03 15:58:02.374: E/AndroidRuntime(22414): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
10-03 15:58:02.374: E/AndroidRuntime(22414): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
10-03 15:58:02.374: E/AndroidRuntime(22414): at java.lang.Thread.run(Thread.java:1019)
10-03 15:58:02.374: E/AndroidRuntime(22414): Caused by: java.lang.NullPointerException
10-03 15:58:02.374: E/AndroidRuntime(22414): at com.androidhive.pushnotifications.JSONParser.getJSONFromUrl(JSONParser.java:81)
10-03 15:58:02.374: E/AndroidRuntime(22414): at com.androidhive.pushnotifications.JSONParser.doInBackground(JSONParser.java:121)
10-03 15:58:02.374: E/AndroidRuntime(22414): at com.androidhive.pushnotifications.JSONParser.doInBackground(JSONParser.java:1)
10-03 15:58:02.374: E/AndroidRuntime(22414): at android.os.AsyncTask$2.call(AsyncTask.java:185)

10-03 15:58:02.374: E/AndroidRuntime(22414): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
10-03 15:58:02.374: E/AndroidRuntime(22414): ... 4 more



I have internet access to server.
It's working on samsung s2(android 4.1.2) but on motorola(with android 2.3.3) not.



Any ideas will by be welcome.




I think the problem is somting with the asynctask... but... nothing more pls help.



PP php server return write answer to the request(php logs) but app cant get it.

No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...