当我从高级Rest客户端从服务(以下代码中的anagrafica
)调用REST api时,它将返回带有预期结果的HTTP响应代码200
。但是,当我从Android应用程序通过具有相同负载的相同服务调用它时,它将返回HTTP响应代码302
。怎么可能?
两个用于说明问题的屏幕截图:
正在从应用程序中调用服务:
try {
String ex = extractUni(getApplicationContext());
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
nameValuePairs.add(new BasicNameValuePair("username", this.username));
nameValuePairs.add(new BasicNameValuePair("password", this.password));
URL address = new URL("https://api.xxx.xx/"+ex+"/anagrafica/");
URLDataReader pp = new URLDataReader(getApplicationContext(), this, nameValuePairs);
pp.execute(address);
} catch (Exception e) {
e.printStackTrace();
CommonUtils cu = new CommonUtils();
cu.sendReport(e, getApplicationContext());
}
这是管理呼叫的类:
public class URLDataReader extends AsyncTask<URL, String, String> {
Context mContext;
OnAsyncTaskComplete listener;
List<NameValuePair> nameValuePairs;
public URLDataReader asyncObject;
CountDownTimer cc;
String url = "";
public URLDataReader(Context context, OnAsyncTaskComplete lisener, List<NameValuePair> nameValuePairs) {
mContext = context;
listener = lisener;
this.nameValuePairs = nameValuePairs;
}
@Override
protected String doInBackground(URL... data) {
HttpClient HttpUrlConnection = new DefaultHttpClient();
HttpParams httpParameters = HttpUrlConnection.getParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, 15000);
HttpConnectionParams.setSoTimeout(httpParameters, 15000);
HttpConnectionParams.setTcpNoDelay(httpParameters, true);
HttpPost httppost = new HttpPost(data[0].toString());
//this.url = httppost.toString();
HttpResponse response = null;
System.setProperty("http.keepAlive", "false");
System.setProperty("java.net.preferIPv4Stack" , "true");
try {
httppost.setEntity(new UrlEncodedFormEntity(this.nameValuePairs));
//SETTA I POST IN GLOBALS
response = HttpUrlConnection.execute(httppost);
} catch (Exception e) {
e.printStackTrace();
CommonUtils cu = new CommonUtils();
cu.sendReport(e, mContext);
}
try {
return convertHttpResponseToString(response);
} catch (Exception e) {
e.printStackTrace();
CommonUtils cu = new CommonUtils();
cu.sendReport(e, mContext);
}
return null;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
//Toast.makeText(mContext, "start timeout", Toast.LENGTH_LONG).show();
asyncObject = this;
cc = new CountDownTimer(10000, 1000) {
public void onTick(long millisUntilFinished) {
int seconds = (int) (millisUntilFinished / 1000);
//Toast.makeText(mContext, seconds+"", Toast.LENGTH_LONG).show();
}
public void onFinish() {
// stop async task if not in progress
if (asyncObject.getStatus() == Status.RUNNING ||
asyncObject.getStatus() == Status.PENDING) {
asyncObject.cancel(true);
cc.cancel();
listener.OnAsyncTaskTimeout();
//Toast.makeText(mContext, "finish", Toast.LENGTH_LONG).show();
}
}
}.start();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
cc.cancel();
listener.OnAsyncTaskComplete(s);
}
private String convertHttpResponseToString(HttpResponse response) throws IOException {
InputStream responseStream = response.getEntity().getContent();
Scanner scanner = new Scanner(responseStream, "UTF-8");
String responseString = scanner.useDelimiter("\\Z").next();
scanner.close();
return responseString;
}
}