I created a code sample to compare the output of the HttpPost method on two different Android OS, v2.3 and v4.0. I have a SIM card that I use the service of a 3G operator. Therefore, I connect to the Internet through a 3G service. The address and port of the proxy server are automatically set by the mobile operator and are the same for both.
My code looks like this:
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());
When I run this application on an Android v2.3 device (Samsung Galaxy S), my output looks like this: log / cat>
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...
So far, when I run the application on Android V4.0.3 (Samsung Galaxy 2 or HTC Xperia), the output is the same in 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...
As you can see, my XML data is wrapped in HTML code. Does this HTML code come from? Someone said, perhaps this is due to the proxy server, which is automatically installed by the mobile operator. Is it correct? I think this should not be right, because if the proxy server controls my code or contributes something to my code in version 4.0, why don't I have the same answer with Android v2.0 and v3.0? Is it because of an error inside Android v4.0? !!!
Any suggestion would be appreciated.