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

先描述一下需求:我为了保证服务端的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);


  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值