当前位置: 首页 > 知识库问答 >
问题:

DefaultHttpClient AsyncTask类超时异常

韦志新
2023-03-14

这是我的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);

    }

}
public class RegisterActivity extends Activity implements MyCallbackInterface{
...
    @Override
    public void onRequestComplete(JSONObject json) {
//hire parse and display json
...
JSONParser mJSONParser1 = new JSONParser(context111);
mJSONParser1.execute(SERVER_URL+"?method=registration&user="+user+"&pass="+pass+"&regId="+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

共有1个答案

陈实
2023-03-14

JSONParser mJSONParser1=new JSONParser(context111);mjsonParser1.execute(server_url+“?method=registration&user=”+user+“&pass=”+pass+“®id=”+regid)输入错误还是直接剪切/粘贴?在“id=”位旁边有一些不寻常的字符。

而且您确实有导致空的代码行,那么为什么它是空的(正如tyczj指出的)

 类似资料:
  • 我在非GUI模式下为100个并发用户运行了一个脚本 我在运行期间保存了生成的 JTL 文件 运行完成后,我在查看结果树侦听器中加载了JTL文件,我可以看到如下错误: 响应代码: 非 HTTP 响应代码: 套接字超时异常 响应消息: 非 HTTP 响应消息: 读取超时 响应代码:非HTTP响应代码:Java . net . connect异常响应消息:非HTTP响应消息:连接超时:连接 我的JMet

  • 我看到以下崩溃在Android应用程序,并没有一个线索,这可能会发生,因为堆栈跟踪没有指向我清楚。你们中有人有线索、想法、暗示这是怎么发生的吗? TimeoutException(@com.android.org.conscrypt.NativeCrypto:X509_free:-2)通过(@com.android.org.conscrypt.OpenSSLX509Certificate:fina

  • 问题内容: 我使用Node.js和TypeScript,并且使用。这是我的测试用例: 我想为整个功能设置一个超时时间。即如果要花费2秒,花费0.5秒,花费5秒,我想在3秒钟后让我抛出错误的超时。 正常调用是一个问题,因为范围丢失了: 而且我不能用普通的方式抓住它: 有关如何解决的任何想法? 问题答案: 您可以使用超时: 您必须将其包装在诺言中才能使用。

  • 我相对来说是android开发的新手。我正在开发一个android应用程序,向web服务器发送请求并解析JSON对象。在与服务器通信时,我经常收到< code > Java . net . socket time out exception:Connection timed out 异常。有时它会完美地工作,没有任何问题。我知道这个问题已经被问过很多次了。但仍然,我没有得到任何满意的解决这个问题的

  • 在每个人告诉我增加超时值之前,我不想增加它,因为100秒不够长是没有意义的。在不引发超时/任务取消异常的情况下,所有其他get和post响应花费的时间更少。 这是密码 这是直接的。有些东西可以打印,有些东西我们不可以 var result=等待客户端。邮递同步(“http://www.foodservice.com/home/fs_login_check.cfm“,内容); 我看不出为什么它会挂在