DefaultHttpClient AsyncTask类超时异常

时间:2013-10-03 14:10:49

标签: java android android-asynctask httpclient

这是我的AsyncTask课程:

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);

    }

}

我在UI(在课程RegisterActivity extends Activity implements MyCallbackInterface中)获得了JSON答案:

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

我从同一个UI(类JSONParser

中调用RegisterActivity extends Activity implements MyCallbackInterface
JSONParser mJSONParser1 = new JSONParser(context111);
mJSONParser1.execute(SERVER_URL+"?method=registration&user="+user+"&pass="+pass+"®Id="+regId);

例外情况:

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

我可以访问服务器的Internet。 它正在使用samsung s2(android 4.1.2),但是在motorola(使用android 2.3.3)上没有。

欢迎任何想法。

我认为这个问题正在与asynctask一起酝酿......但是......没有更多的帮助。

PP php服务器返回写请求的回答(php日志),但app不能得到它。

2 个答案:

答案 0 :(得分:0)

JSONParser mJSONParser1 = new JSONParser(context111); mJSONParser1.execute(SERVER_URL+"?method=registration&user="+user+"&pass="+pass+"®Id="+regId)拼写错误还是直接剪切/粘贴?你在“Id =”位旁边有一些不寻常的字符。

你确实有一行代码导致null,所以为什么它为null(如tyczj指出)

答案 1 :(得分:0)

上面的答案是对的,但是你应该使用POST而不是GET来保证安全性

SomeSwiftClass *swiftVar = [SomeSwiftClass new];
SomeObjectiveCClass *objVar = [[SomeObjectiveCClass alloc] initWithSwiftClass:swiftVar]

并设置Params out就像这样

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("SERVER_URL");
httpPost.setEntity(new UrlEncodedFormEntity(params));

GET与POST here

有很好的争论