org.apache.http.client.ClientProtocolException解決方案
阿新 • • 發佈:2019-02-14
先描述一下需求:我為了保證服務端的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): 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);