gpt4 book ai didi

Android,为什么 HttpPost 方法在 Android v2.3 和 Android v4.0.3 中返回两个不同的结果?

转载 作者:太空宇宙 更新时间:2023-11-03 11:19:17 26 4
gpt4 key购买 nike

我创建了一个示例代码来比较 HttpPost 方法在两个不同的 Android 操作系统 v2.3 和 v4.0 中的输出。我有一张 sim 卡,我正在使用运营商的 3G 服务。因此,我通过 3G 服务连接到 Internet。代理服务器地址和端口由移动运营商自动设置,两者相同。

我的代码是这样的:

public class MainActivity extends Activity {

private final String TAG = "MainActivity";

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);


Log.i(TAG, "inside onCreate()...");


ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connMgr.getActiveNetworkInfo();

if (activeNetwork != null) {
Log.i(TAG, "Internet connection found.");
new MyAsyncTask().execute();
} else
Log.i(TAG, "Internet connection not found.");
}

private class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute()...");
}

@Override
protected Boolean doInBackground(Void... arg0) {
boolean status = false;

String xml = postData();
if(xml != null)
status = true;

return status;
}

@Override
protected void onPostExecute(Boolean result) {
if (result)
Log.i(TAG, "Process finished successfully...");
}
}

private String postData() {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://demo.excelforce.com.my/mobiletraderjssb/dopwd.aspx?");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("u", Uri.encode("test01")));
nameValuePairs.add(new BasicNameValuePair("p", Encryption.encrypt("112233")));
nameValuePairs.add(new BasicNameValuePair("v", "1.1"));
nameValuePairs.add(new BasicNameValuePair("t", "0"));

try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.i("Requestd Connection", httppost.getURI().toString());


HttpResponse response = httpClient.execute(httppost);
String responseBody = EntityUtils.toString(response.getEntity());
Log.i("Server Response: ", responseBody);

if (response.containsHeader("Set-Cookie")) {
String sessionId = extractSessionId(response.getHeaders("Set-Cookie")[0].getValue());
// PropertyManager.setSessionID(sessionId);
Log.i("Session Id:", sessionId);
} else
Log.e("ERROR", "Response doesn't have Set-Cookie in header");

return responseBody;

} catch(UnsupportedEncodingException usee) {
usee.printStackTrace();
} catch(ClientProtocolException cpe) {
cpe.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
}

return null;
}

private String extractSessionId(String str) {
String session = null;
if (str!=null)
session = str.substring(0, str.indexOf(";"));
return session;
}
}

当我在 Android v2.3 设备 (Samsung Galaxy S) 上运行这个应用程序时,我在 logcat 中的输出是这样的:

07-09 18:21:11.031: I/MainActivity(1277): inside onCreate()...
07-09 18:21:11.035: I/MainActivity(1277): Internet connection found.
07-09 18:21:11.035: I/MainActivity(1277): onPreExecute()...
07-09 18:21:11.718: I/Requestd Connection(1277): http://demo.excelforce.com.my/mobiletraderjssb/dopwd.aspx?
07-09 18:21:13.941: I/Server Response:(1277): <TD ID="LGFLAG">S</TD><TD ID="LGMSG"></TD><TD ID="CLNT_0">CLNTXXX,EF TEST,C,001,</TD>
07-09 18:21:13.941: I/Session Id:(1277): ASPNETSESSIONID=jjodj3m22notn1m50oxs0u55
07-09 18:21:13.941: I/MainActivity(1277): Process finished successfully...

而当我在 Android V4.0.3(Samsung Galaxy 2 或 HTC Xperia)上运行应用程序时,logcat 中的输出如下所示:

07-09 18:08:45.085: I/MainActivity(19358): inside onCreate()...
07-09 18:08:45.090: I/MainActivity(19358): Internet connection found.
07-09 18:08:45.090: I/MainActivity(19358): onPreExecute()...
07-09 18:08:45.155: I/Requestd Connection(19358): http://demo.excelforce.com.my/mobiletraderjssb/dopwd.aspx?
07-09 18:08:46.235: I/Server Response:(19358): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
07-09 18:08:46.235: I/Server Response:(19358): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
07-09 18:08:46.235: I/Server Response:(19358): <html xmlns="http://www.w3.org/1999/xhtml">
07-09 18:08:46.235: I/Server Response:(19358): <head>
07-09 18:08:46.235: I/Server Response:(19358): <title></title>
07-09 18:08:46.235: I/Server Response:(19358): </head>
07-09 18:08:46.235: I/Server Response:(19358): <body>
07-09 18:08:46.235: I/Server Response:(19358): <table>
07-09 18:08:46.235: I/Server Response:(19358): <tr>
07-09 18:08:46.235: I/Server Response:(19358): <td id="LGFLAG">S</td>
07-09 18:08:46.235: I/Server Response:(19358): <td id="LGMSG"></td>
07-09 18:08:46.235: I/Server Response:(19358): <td id="CLNT_0">CLNTXXX,EF TEST,C,001,</td>
07-09 18:08:46.235: I/Server Response:(19358): </tr>
07-09 18:08:46.235: I/Server Response:(19358): </table>
07-09 18:08:46.235: I/Server Response:(19358): </body>
07-09 18:08:46.235: I/Server Response:(19358): </html>
07-09 18:08:46.235: E/ERROR(19358): Response doesn't have Set-Cookie in header
07-09 18:08:46.440: I/MainActivity(19358): Process finished successfully...

如您所见,我的 XML 数据由 HTML 代码包装。这段 HTML 代码来自哪里?有人说可能是因为移动运营商自动设置的代理服务器。这是正确的吗?我认为它不应该是正确的,因为如果代理服务器在 v4.0.3 中操纵我的代码或向我的代码中注入(inject)某些内容,为什么我对 Android v2.0 和 v3.0 没有相同的响应?是因为 Android v4.0 内部的 bug 吗?!!!

如有任何建议,我们将不胜感激。

最佳答案

它看起来像缺少接受请求的竞争类型。

尝试添加如下内容:

httppost.setHeader("Accept","application/xml");

如果没有,则设置 Apache HttpClient 登录:
https://stackoverflow.com/a/3303131/538169
并寻找请求的差异。

关于Android,为什么 HttpPost 方法在 Android v2.3 和 Android v4.0.3 中返回两个不同的结果?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11393644/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com