AsyncTask HttpPost 在 3G 上执行失败,但在 Wifi 上工作 [英] AsyncTask HttpPost execute fails on 3G, but works on Wifi

查看:18
本文介绍了AsyncTask HttpPost 在 3G 上执行失败,但在 Wifi 上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将一些字符串的 Http post 发送到 Web 服务.我正在使用 KSoap.

I need to do a Http post of some strings to a web service. I am using KSoap.

@Override
protected JSONObject doInBackground(JSONObject... params) {
    String result;

    HttpParams httpParameters = new BasicHttpParams();
    // Set the timeout in milliseconds until a connection is established.
    // The default value is zero, that means the timeout is not used. 
    int timeoutConnection = 30000;
    HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
    // Set the default socket timeout (SO_TIMEOUT) 
    // in milliseconds which is the timeout for waiting for data.
    int timeoutSocket = 50000;
    HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

    HttpClient httpclient = new DefaultHttpClient(httpParameters);
    setupClient(httpclient);
    //HttpURLConnection httpclient = new HttpURLConnection(_url);

    Log.d(TAG, "Posting to ["+_url+"]");
    HttpPost postdata = new HttpPost(_url);

    ...set up parameters

    ResponseHandler<String> handler = new BasicResponseHandler();  
    Calendar c = Calendar.getInstance();
    long start = c.getTimeInMillis();
    try {               
        Log.d(TAG, "Starting post data call ["+_url+"]");           
        result = httpclient.execute(postdata,handler);
        c = Calendar.getInstance();
        long total = c.getTimeInMillis() - start;
        Log.d(TAG, "Finished post data call ["+result+"] in ["+total+"] millis");
    }catch(HttpResponseException e)
    {
        c = Calendar.getInstance();
        long total = c.getTimeInMillis() - start;
        Log.e(TAG, "HttpResponseException: ["+total+"] millis. There was a problem communicating: " + e.getMessage());
        return null;
    }catch (ClientProtocolException e) {
        Log.e(TAG, "ClientProtocolException There was a problem getting to the service: " + e.getMessage());
        return null;
    } catch(SocketTimeoutException e){
        Log.e(TAG, "SocketTimeoutException There was a problem connecting: " + e.getMessage());
        return null;        
    } catch (IOException e) {
        Log.e(TAG, "IOException There was a problem reading the data: " + e.getMessage());
        return null; 
    }  
    catch(Exception e)
    {
        Log.e(TAG, "Exception An error occurred: " + e.toString());
        return null;
    }
    httpclient.getConnectionManager().shutdown();
    JSONObject resp = null;
    try {
        resp = new JSONObject(result);          
    } catch (JSONException e) {
        resp = null;
        e.printStackTrace();
    }

    Log.i(TAG, result.toString());  
    return resp;
}

所以当我使用 Wifi 连接时这很好用,但是使用 3G 在 16 - 18 秒内失败并抛出 HttpResponseException.

So this works fine when I use Wifi to connect, but using 3G is fails in 16 - 18 seconds throwing a HttpResponseException.

我无法相信是 DefaultHttpClient 运行异步是问题所在,因为它也应该在 Wifi 上抛出错误.

I cannot believe that it's the DefaultHttpClient running async that is the issue, because then it should throw the error on Wifi as well.


编辑
Web 服务是用 C# 编写的,并使用 JSON 响应.


EDIT
The web services are written in C#, and use JSON responses.

这里要求的是 logcat,在 18 秒内给出 HttpResponseException,正如我在原始问题中所说:

As requested here is the logcat, giving the HttpResponseException in 18 seconds, as I said in the original question:

07-22 11:13:04.678: D/ProgressBar(26524): setProgress = 0<br/>
07-22 11:13:04.678: D/ProgressBar(26524): setProgress = 0, fromUser = false<br/>
07-22 11:13:04.678: D/ProgressBar(26524): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000<br/>
07-22 11:13:31.968: D/GpsLocation(26524): Got location<br/>
07-22 11:13:32.018: D/WebService(26524): BASE ADDRESS [http://[SERVICEURL]]<br/>
07-22 11:13:32.023: D/ProgressBar(26524): setProgress = 0<br/>
07-22 11:13:32.023: D/ProgressBar(26524): setProgress = 0, fromUser = false<br/>
07-22 11:13:32.023: D/ProgressBar(26524): mProgress = 0mIndeterminate = false, mMin = 0, mMax = 10000<br/>
07-22 11:13:32.048: D/HttpPostWebServiceTask(26524): Posting to [http://[SERVICEURL]/Authentication/Authenticate]<br/>
07-22 11:13:32.053: D/HttpPostWebServiceTask(26524): Starting post data call [http://[SERVICEURL]/Authentication/Authenticate]<br/>
07-22 11:13:47.758: D/dalvikvm(26524): GC_CONCURRENT freed 258K, 17% free 8062K/9671K, paused 22ms+25ms, total 157ms<br/>
**07-22 11:13:50.728: E/HttpPostWebServiceTask(26524): HttpResponseException: [18671] millis. There was a problem communicating: Gateway Timeout**<br/>
07-22 11:13:50.778: D/GOT RESPONSE(26524): NULL RESPONSE FROM SERVER<br/>
07-22 11:13:50.778: E/HttpPostWebServiceTask(26524): Error invoking command: null<br/>
07-22 11:13:50.783: W/System.err(26524): java.lang.NullPointerException<br/>
07-22 11:13:50.793: W/System.err(26524):    at com.app.webservices.WebService.checkValidResponse(WebService.java:82)<br/>
07-22 11:13:50.793: W/System.err(26524):    at com.app.webservices.WebService.access$0(WebService.java:76)<br/>
07-22 11:13:50.793: W/System.err(26524):    at com.app.webservices.WebService$1.execute(WebService.java:120)<br/>
07-22 11:13:50.793: W/System.err(26524):    at com.app.webservices.WebService$1.execute(WebService.java:1)<br/>
07-22 11:13:50.793: W/System.err(26524):    at com.blm.android.webservice.HttpPostWebServiceTask.onPostExecute(HttpPostWebServiceTask.java:226)<br/>
07-22 11:13:50.793: W/System.err(26524):    at com.blm.android.webservice.HttpPostWebServiceTask.onPostExecute(HttpPostWebServiceTask.java:1)<br/>
07-22 11:13:50.798: W/System.err(26524):    at android.os.AsyncTask.finish(AsyncTask.java:631)<br/>
07-22 11:13:50.798: W/System.err(26524):    at android.os.AsyncTask.access$600(AsyncTask.java:177)<br/>
07-22 11:13:50.803: W/System.err(26524):    at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)<br/>
07-22 11:13:50.803: W/System.err(26524):    at android.os.Handler.dispatchMessage(Handler.java:99)<br/>
07-22 11:13:50.803: W/System.err(26524):    at android.os.Looper.loop(Looper.java:137)<br/>
07-22 11:13:50.803: W/System.err(26524):    at android.app.ActivityThread.main(ActivityThread.java:4947)<br/>
07-22 11:13:50.803: W/System.err(26524):    at java.lang.reflect.Method.invokeNative(Native Method)<br/>
07-22 11:13:50.803: W/System.err(26524):    at java.lang.reflect.Method.invoke(Method.java:511)<br/>
07-22 11:13:50.803: W/System.err(26524):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)<br/>
07-22 11:13:50.808: W/System.err(26524):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)<br/>
07-22 11:13:50.808: W/System.err(26524):    at dalvik.system.NativeStart.main(Native Method)<br/>


结束编辑

这是我应该走的路吗:http://www.vogella.com/articles/AndroidNetworking/article.html
那么我如何使用这种方法发布数据实体?


END EDIT

Is this the way I should go: http://www.vogella.com/articles/AndroidNetworking/article.html
Then how do I post data entities using this method?

推荐答案

您可以尝试通过手机的 Web 浏览器连接到您的服务器,以查看问题是在您的应用程序内还是 3G 问题正在解决/连接到您服务器的地址.

You could try to connect to your server via the web browser of your phone to see if the problem is within your application or if the problem is a 3G problem resolving/connecting to the address of your server.

我的猜测是您将无法通过网络浏览器进行连接.

My guess is that you won't be able to connect either via a web browser.

您的网络服务托管在哪里?它应该可以从外部网络访问吗?如果服务器在同一网络中,您也许可以通过 wifi 连接到您的服务器.

Where is hosted your web service? Should it be accessible from an external network ? You may be able to connect to your server via wifi if the server is in the same network.

这篇关于AsyncTask HttpPost 在 3G 上执行失败,但在 Wifi 上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆