org.apache.http.client.ClientProtocolException解決方案

來源:互聯網
上載者:User
先描述一下需求:我為了保證服務端的session逾時問題,然後做了一個timetask的輪詢,每20分鐘調用一次服務端。其中String url = new PropertyUtil(this).getUrlBase();是我們web端的首頁,我隨便調用了一下只是為了保證sessin的問題。
串連的第一次是正常返回,然後剩下的每次都報這樣的錯:
12-03 18:07:09.470: W/System.err(5047): org.apache.http.client.ClientProtocolException
12-03 18:07:09.470: W/System.err(5047):         at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:557)
12-03 18:07:09.470: W/System.err(5047):         at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
12-03 18:07:09.470: W/System.err(5047):         at com.loopj.android.http.AsyncHttpRequest.makeRequest(AsyncHttpRequest.java:74)
12-03 18:07:09.470: W/System.err(5047):         at com.loopj.android.http.AsyncHttpRequest.makeRequestWithRetries(AsyncHttpRequest.java:91)
12-03 18:07:09.470: W/System.err(5047):         at com.loopj.android.http.AsyncHttpRequest.run(AsyncHttpRequest.java:54)
12-03 18:07:09.470: W/System.err(5047):         at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:442)
12-03 18:07:09.470: W/System.err(5047):         at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-03 18:07:09.470: W/System.err(5047):         at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-03 18:07:09.470: W/System.err(5047):         at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
12-03 18:07:09.470: W/System.err(5047):         at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
12-03 18:07:09.470: W/System.err(5047):         at java.lang.Thread.run(Thread.java:856)
12-03 18:07:09.470: W/System.err(5047): Caused by: org.apache.http.client.CircularRedirectException: Circular redirect to 'http://192.168.1.110:8080/task/'
12-03 18:07:09.470: W/System.err(5047):         at org.apache.http.impl.client.DefaultRedirectHandler.getLocationURI(DefaultRedirectHandler.java:173)
12-03 18:07:09.470: W/System.err(5047):         at org.apache.http.impl.client.DefaultRequestDirector.handleResponse(DefaultRequestDirector.java:923)
12-03 18:07:09.470: W/System.err(5047):         at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:475)
12-03 18:07:09.470: W/System.err(5047):         at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)

12-03 18:07:09.470: W/System.err(5047):         ... 10 more


package cn.timetask.service; import java.net.URLEncoder; import org.apache.http.Header;import org.apache.http.client.params.ClientPNames;import org.apache.http.protocol.HTTP;import org.json.JSONException;import org.json.JSONObject; import cn.timetask.app.App;import cn.timetask.dict.ResultCode;import cn.timetask.utils.AsyncHttpCilentUtil;import cn.timetask.utils.LogUtil;import cn.timetask.utils.NetworkUtil;import cn.timetask.utils.PropertyUtil;import cn.timetask.utils.StringUtil; import com.loopj.android.http.AsyncHttpClient;import com.loopj.android.http.AsyncHttpResponseHandler;import com.loopj.android.http.RequestParams; import android.app.Notification;import android.app.NotificationManager;import android.app.PendingIntent;import android.app.Service;import android.content.Intent;import android.os.IBinder;import android.util.Base64;import android.util.Log; /** * 為保持web端session逾時的問題 將採用20分鐘訪問一次服務端 *  * @author linhui 2013-12-3上午9:56:11 */public class KeepWebSessionService extends Service {        public static final String TAG = "KeepWebSession";        public static final String ACTION = "cn.timetask.service.KeepWebSessionService";         @Override        public IBinder onBind(Intent intent) {                return null;        }         @Override        public void onCreate() {        }         @Override        public void onStart(Intent intent, int startId) {                LogUtil.d(TAG, "===keep service start===");                new Thread() {                        public void run() {                                keepWebSession();                        };                }.start();        }         public void keepWebSession() {                // 非ui線程                AsyncHttpClient client = AsyncHttpCilentUtil.getInstence();                RequestParams rparams = new RequestParams();                String url = new PropertyUtil(this).getUrlBase();                client.setBasicAuth(App.getInstance().getUserCode(), App.getInstance()                                .getPassword());                rparams.put(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, false);                client.setUserAgent("Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1;.NET CLR 1.1.4322; CIBA; .NET CLR 2.0.50727)");                client.post(url, rparams, new AsyncHttpResponseHandler() {                        @Override                        public void onSuccess(String response) {                                try {                                        LogUtil.d("success response", response);                                        JSONObject jsonObj = new JSONObject(response);                                        if (StringUtil.notEmpty(jsonObj)) {                                                LogUtil.d(TAG, "=====keep web session=====" + response);                                        }                                } catch (JSONException e) {                                        LogUtil.e(TAG, e + "");                                        e.printStackTrace();                                }                        }                         @Override                        public void onStart() {                                super.onStart();                                LogUtil.e(TAG, "onStart");                        }                         @Override                        public void onFinish() {                                super.onFinish();                                LogUtil.e(TAG, "onFinish");                        }                         @Override                        public void onFailure(int arg0, Header[] arg1, byte[] arg2,                                        Throwable arg3) {                                super.onFailure(arg0, arg1, arg2, arg3);                                LogUtil.e(TAG, "onFailure");                                LogUtil.d("arg0", arg0 + "");                                LogUtil.d("arg3", arg3 + "");                        }                });         }         @Override        public void onDestroy() {                super.onDestroy();                LogUtil.d(TAG, "===keep service onDestroy===");         } }

解決方案:

這個是因為重新導向引起的,然後我在加了一個介面,專門用來返回一個json即可。

你可以在服務端返回retur null,或者返回一個json等,只要別重新導向就行。

還有一個問題就是,當你報了一次500,或者timetou後,會出現重新導向的問題,我懷疑這是location和自己本事的utl衝突導致的,具體的資料我還沒有找到,請記得加上下面這句,以免在服務端session逾時後,再次調用導致重新導向的問題

AsyncHttpClient client = AsyncHttpCilentUtil.getInstence();client.getHttpClient().getParams().setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, true);


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.