I have created a sample code to compare output of HttpPost method in two different Android OSs, v2.3 and v4.0. I have a sim-card that I'm using 3G service of an operator. Therefore, I'm connecting to Internet through 3G service. Proxy server address and port is set automatically by mobile operator and is same for both of them.
My code is 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());
// 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;
}
}
When I run this application on Android v2.3 device (Samsung Galaxy S), my output is like this in 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...
While, when I run the application on Android V4.0.3 (Samsung Galaxy 2 or HTC Xperia), output is like this 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 by HTML Code. This HTML code comes from where? Someone said maybe it is because of Proxy server which is set automatically by mobile operator. Is it correct? I think it shouldn't be correct because if proxy server manipulate my code or inject something to my code in v4.0.3 why I don't have same response with Android v2.0 and v3.0? Is it because of bug inside Android v4.0?!!!
Any suggestion would be appreciated.