几年前,我开发了一个应用程序。在每个活动中,我都使用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操作系统上获得):
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中,代理设置会影响接收到的数据吗?提到代理服务器可能会注入这些代码并包装我想要的数据。我认为这个想法是不正确的,因为在相同的情况下,当我使用一个sim卡(3G)为两个Android V2.0。V3.0和v4.0,虽然它们的代理设置都相同,但我在v4.0中收到了错误的数据。我想安卓v4.0的HttpPost库有问题!
这里发生了什么?此代码适用于4.0版以下的Android,而不适用于4.0及以上的Android!
你有这样的经历吗?你做了什么?如有任何建议,我们将不胜感激。感谢
您是否考虑过该问题是否与在主线程上运行网络操作有关?您不能在蜂窝(3.0)和转发的主线程上执行网络操作。
您必须使用AsycTask
http://developer.android.com/reference/android/os/AsyncTask.html
或者一个新的线程来管理http连接。不能使用主踏板进行呼叫。
AsyctTask和Thread基本相同,但AsycTask实现了4个基本方法。
onPreExecute()、onPostExecute、onProgressUpdate和doInBackground。
试试它们,告诉我们它是否有效。