2

Some years ago, i developed an application. In each activity I was using HttpPost method to send some parameters to server. It is/was working fine with 3G service on all devices with below Android v4.0. My code is like this:

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;
}

Expected result in logcat (which I can get it on Android OS below v4.0):

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

However, just when i'm connecting to internet through 3G service and Android version 4.0.3 and 4.0.4, my output in logcat is like this:

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

Actually, I have no idea this HTML code comes from where! because for connecting to 3G, mobile operator set proxy for mobile phone automatically. We suspicious to proxy settings. Someone, in Java, does proxy setting affect received data? mentioned that maybe proxy server injects this code and wrap my desired data. I think this idea is not correct because in same situation when I'm using one sim-card (3G) for both of Android V2.0. V3.0 and v4.0 while proxy setting is same for all of them, I receive wrong data in v4.0. I guess something with HttpPost library of Android v4.0 is wrong!

What is happening here? This code is working on Android below v4.0 while it doesn't work on Android v4.0 and above!

Did you have an experience like this? What did you do? Any suggestions would be appreciated. Thanks

7
  • Could you give a description of what happens on android 4.0 when the code runs? Also could you give a link to an url that crash?
    – Warpzit
    Jul 9, 2012 at 9:04
  • Thanks dear Warpzit, Actually there is no crash and application works. but i have unexpected data which wraps my data. We thought may be its because of Mobile service provider which sets proxy setting automatically on mobile devices. However, it seems it is not correct because when i run the application on Android 2.2 or 3.0, it works fine without problem. But, if I run the application on Android 4.0 (same sim-card/mobile 3g service provider) my XML data will be wrapped with HTML code (as you can see in above log results). Therefore, it shows proxy server doesn't manipulate the code.
    – Hesam
    Jul 9, 2012 at 9:45
  • Therefore, the only difference will be Android OS and I think something is wrong with Android v4.0
    – Hesam
    Jul 9, 2012 at 9:46
  • I'm pretty sure android 4.0 doesn't wrap httpget with html code... that would pretty much break everything that uses httpget. There must be something else you could look at.
    – Warpzit
    Jul 9, 2012 at 9:48
  • Could you try to give example output from httpget with 2.3 and 4.0?
    – Warpzit
    Jul 9, 2012 at 9:48

2 Answers 2

1

Have you considered if the issue could be related to running a network operation on the main thread? You can't perform networking operations on main thread from honeycomb (3.0) and forward.

1
  • definitely you are right, I know this rule and all of above code is running under AsyncTask class in doInBackground() method. This is not my problem. However, thanks for sharing your idea :)
    – Hesam
    Jul 9, 2012 at 7:33
-1

You have to use AsycTask

http://developer.android.com/reference/android/os/AsyncTask.html

or a new Thread to manage the http connections. You cannot use the main tread to make a call.

AsyctTask and Thread are mostly the same but AsycTask implements 4 basic methods .

onPreExecute(), onPostExecute, onProgressUpdate and doInBackground.

Try them and tell us if it worked.

2
  • 1
    definitely you are right, I know this rule and all of above code is running under AsyncTask class in doInBackground() method. This is not my problem. However, thanks for sharing your idea :)
    – Hesam
    Jul 9, 2012 at 7:34
  • I´ll try something and give you another aproach. Hope you solve it by the way. :)
    – reixa
    Jul 9, 2012 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.