几年前,我开发了一个应用程序。在每个活动中,我都使用 HttpPost 方法将一些参数发送到服务器。在 Android v4.0 以下的所有设备上使用 3G 服务运行良好。我的代码是这样的:
private String postData() {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PropertyManager.getLoginURL());
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(4);
nameValuePairs.add(new BasicNameValuePair("u", Uri.encode(PropertyManager.getUserId())));
nameValuePairs.add(new BasicNameValuePair("p", Encryption.encrypt(PropertyManager.getPassword())));
nameValuePairs.add(new BasicNameValuePair("v", PropertyManager.VER));
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);
}
String str = responseBody.substring(0, 9);
if(str.equalsIgnoreCase("<!DOCTYPE")) {
Log.i("postData()", "HTML found instead of XML. Try to extract info...");
str = responseBody.substring(responseBody.indexOf("<tr>")+4, responseBody.lastIndexOf("</tr>"));
str = str.toUpperCase();
Log.i("Extracted info", str);
return str;
} else
return responseBody;
} catch(UnsupportedEncodingException usee) {
usee.printStackTrace();
} catch(ClientProtocolException cpe) {
cpe.printStackTrace();
} catch(IOException ioe) {
ioe.printStackTrace();
}
return null;
}
logcat 中的预期结果(我可以在 v4.0 以下的 Android OS 上得到它):
07-09 12:38:49.593: I/JupiterMPro(4273): onCreate executes ...
07-09 12:38:49.601: I/MSBootUp(4273): inside onCreate();
07-09 12:38:49.694: I/MA_isOnline(4273): Checking Internet Connection...
07-09 12:38:49.698: I/MA_isOnline(4273): Internet Connection found.
07-09 12:38:49.730: I/JupiterMPro(4273): onDestroy executes ...
07-09 12:39:40.675: I/MA_isOnline(4273): Checking Internet Connection...
07-09 12:39:40.679: I/MA_isOnline(4273): Internet Connection found.
07-09 12:39:40.820: I/Requestd Connection(4273): http://demo.excelforce.com.my/mobiletraderjssb/dopwd.aspx?
07-09 12:39:44.609: I/Server Response:(4273): <TD ID="LGFLAG">S</TD><TD ID="LGMSG"></TD><TD ID="CLNT_0">CLNTXXX,EF TEST,C,001,</TD>
07-09 12:39:44.613: I/Session Id:(4273): ASPNETSESSIONID=zkuus2n304jnkubi4e1nqnuw
07-09 12:39:44.714: I/JupiterMPro(4273): onCreate executes ...
07-09 12:39:45.140: I/MSMarket(4273): Creating activity...
07-09 12:39:45.202: I/JupiterMPro(4273): onStart executes ...
07-09 12:39:45.202: I/JupiterMPro(4273): onResume executes ...
07-09 12:39:45.241: I/MSMarket, Requested URL:(4273): http://demo.excelforce.com.my/mobiletraderjssb/mtjvg.aspx?marketsummary&msg=1&brd=0
07-09 12:39:45.601: I/MSLogin(4273): onStop executes ...
07-09 12:39:46.702: I/MSMarket, Response code:(4273): 200
07-09 12:39:46.718: I/MSMarket, Server Response:(4273): 50,9/7/2012*12:39 PM#1619.61&-0.94&0&448.392m&399.033m&264&331&293&682&1&V
07-09 12:39:46.855: I/Timer(4273): Timer executes...
07-09 12:39:46.855: I/Timer(4273): seconds elapsed: 1
07-09 12:39:47.757: I/Timer(4273): Timer executes...
07-09 12:39:47.761: I/Timer(4273): seconds elapsed: 2
07-09 12:39:48.757: I/Timer(4273): Timer executes...
07-09 12:39:48.761: I/Timer(4273): seconds elapsed: 3
但是,当我通过 3G 服务和 Android 版本 4.0.3 和 4.0.4 连接到互联网时,我在 logcat 中的输出是这样的:
07-09 14:19:15.395: I/JupiterMPro(4750): onCreate executes ...
07-09 14:19:15.410: I/MSBootUp(4750): inside onCreate();
07-09 14:19:15.465: I/MA_isOnline(4750): Checking Internet Connection...
07-09 14:19:15.465: I/MA_isOnline(4750): Internet Connection found.
07-09 14:19:15.560: I/JupiterMPro(4750): onDestroy executes ...
07-09 14:19:43.765: I/MA_isOnline(4750): Checking Internet Connection...
07-09 14:19:43.765: I/MA_isOnline(4750): Internet Connection found.
07-09 14:19:44.015: I/Requestd Connection(4750): http://demo.excelforce.com.my/mobiletraderjssb/dopwd.aspx?
07-09 14:20:09.350: I/Server Response:(4750): <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
07-09 14:20:09.350: I/Server Response:(4750): "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
07-09 14:20:09.350: I/Server Response:(4750): <html xmlns="http://www.w3.org/1999/xhtml">
07-09 14:20:09.350: I/Server Response:(4750): <head>
07-09 14:20:09.350: I/Server Response:(4750): <title></title>
07-09 14:20:09.350: I/Server Response:(4750): </head>
07-09 14:20:09.350: I/Server Response:(4750): <body>
07-09 14:20:09.350: I/Server Response:(4750): <table>
07-09 14:20:09.350: I/Server Response:(4750): <tr>
07-09 14:20:09.350: I/Server Response:(4750): <td id="LGFLAG">S</td>
07-09 14:20:09.350: I/Server Response:(4750): <td id="LGMSG"></td>
07-09 14:20:09.350: I/Server Response:(4750): <td id="CLNT_0">CLNTXXX,EF TEST,C,001,</td>
07-09 14:20:09.350: I/Server Response:(4750): </tr>
07-09 14:20:09.350: I/Server Response:(4750): </table>
07-09 14:20:09.350: I/Server Response:(4750): </body>
07-09 14:20:09.350: I/Server Response:(4750): </html>
07-09 14:20:09.350: I/postData()(4750): HTML found instead of XML. Try to extract info...
07-09 14:20:09.365: I/Extracted info(4750): <TD ID="LGFLAG">S</TD>
07-09 14:20:09.365: I/Extracted info(4750): <TD ID="LGMSG"></TD>
07-09 14:20:09.365: I/Extracted info(4750): <TD ID="CLNT_0">CLNTXXX,EF TEST,C,001,</TD>
07-09 14:20:09.460: I/JupiterMPro(4750): onCreate executes ...
07-09 14:20:09.510: I/webclipboard(4750): clipservice: android.sec.clipboard.ClipboardExManager@41994c30
07-09 14:20:09.615: I/MSMarket(4750): Creating activity...
07-09 14:20:09.685: I/JupiterMPro(4750): onStart executes ...
07-09 14:20:09.685: I/JupiterMPro(4750): onResume executes ...
07-09 14:20:09.755: I/MSMarket, Requested URL:(4750): http://demo.excelforce.com.my/mobiletraderjssb/mtjvg.aspx?marketsummary&msg=1&brd=0
07-09 14:20:10.030: I/MSLogin(4750): onStop executes ...
07-09 14:20:11.350: I/MSMarket, Response code:(4750): 200
07-09 14:20:11.365: I/MSMarket, Server Response:(4750): 50,9/7/2012*2:20 PM#1619.61&-0.94&0&448.392m&399.033m&264&331&293&682&1&P
实际上,我不知道这段 HTML 代码来自哪里!因为为了连接3G,移动运营商会自动为手机设置代理。我们怀疑代理设置。有人,在Java中,代理设置会影响接收到的数据吗?提到也许代理服务器会注入此代码并包装我想要的数据。我认为这个想法是不正确的,因为在相同的情况下,当我为两个 Android V2.0 使用一张 sim 卡 (3G) 时。V3.0 和 v4.0 虽然代理设置对所有这些都相同,但我在 v4.0 中收到错误的数据。我猜想 Android v4.0 的 HttpPost 库有问题!
这里发生了什么?此代码适用于 v4.0 以下的 Android,而不适用于 Android v4.0 及更高版本!
你有过这样的经历吗?你做了什么?任何建议,将不胜感激。谢谢