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);