4

我创建了一个示例代码来比较 HttpPost 方法在两个不同的 Android 操作系统 v2.3 和 v4.0 中的输出。我有一张使用运营商 3G 服务的 sim 卡。因此,我通过 3G 服务连接到 Internet。代理服务器地址和端口由移动运营商自动设置,两者相同。

我的代码是这样的:

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

当我在 Android v2.3 设备(Samsung Galaxy S)上运行这个应用程序时,我的输出在 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...

而当我在 Android V4.0.3(Samsung Galaxy 2 或 HTC Xperia)上运行应用程序时,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...

如您所见,我的 XML 数据由 HTML 代码包装。这个 HTML 代码来自哪里?有人说可能是因为移动运营商自动设置的代理服务器。这是对的吗?我认为它不应该是正确的,因为如果代理服务器在 v4.0.3 中操纵我的代码或向我的代码注入一些东西,为什么我对 Android v2.0 和 v3.0 没有相同的响应?是因为Android v4.0里面的bug吗?!!!

任何建议将不胜感激。

4

1 回答 1

0

看起来缺少接受请求类型。

尝试添加类似的内容:

httppost.setHeader("Accept","application/xml");

如果没有,则设置 Apache HttpClient 登录:
https
://stackoverflow.com/a/3303131/538169 并查找请求中的差异。

于 2012-07-09T11:05:48.437 回答