gpt4 book ai didi

java - DefaultHttpClient AsyncTask 类超时异常

转载 作者:行者123 更新时间:2023-12-01 04:19:00 26 4
gpt4 key购买 nike

这是我的 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 中得到了 JSON 答案(在类 RegisterActivity extends Activity Implements MyCallbackInterface 中):

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

我从同一 UI 调用 JSONParser(RegisterActivity 类扩展 Activity 实现 MyCallbackInterface)

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

异常(exception)情况:

10-03 15:58:02.303: W/System.err(22414): java.net.SocketException: Connection timed out10-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 #110-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.NullPointerException10-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

我可以通过互联网访问服务器。它可以在 samsung s2(android 4.1.2) 上运行,但在 motorola(android 2.3.3) 上不行。

欢迎任何想法。

我认为问题出在异步任务上......但是......仅此而已,请帮忙。

PP php 服务器返回对请求的写入答案(php 日志),但应用程序无法获取它。

最佳答案

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

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

关于java - DefaultHttpClient AsyncTask 类超时异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19161511/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com