从HttpPost Android中提取网址

时间:2012-08-20 18:39:20

标签: java android http http-post

我有一个使用nameValuePairs构建的长网址,而且我目前正试图找出为什么该帖子导致某些设备上出现500错误而其他设备上出现200错误设备。我需要从httppost中提取完整的网址,但据我所知,它应该采用以下格式:

http://xx.com/site.asmx?var1=blah?var2=blah

同样的版本适用于手机而不适用​​于平板电脑。我已经尝试查看我的代码并抓住平板电脑可能无法定义此类变量的任何地方:

final TelephonyManager tm = (TelephonyManager) getBaseContext()
                .getSystemService(Context.TELEPHONY_SERVICE);
        String tmDevice;
        try{
        tmDevice = "" + tm.getDeviceId().toString();
        }
        catch (NullPointerException e)
        {
            tmDevice = "null";
        }

以下是nameValuePairs的一些内容以及代码发布部分的摘录:

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs
            .add(new BasicNameValuePair("CurrentOwnerUsername", name));
    nameValuePairs.add(new BasicNameValuePair("OSVersion",
            Build.VERSION.RELEASE));
    nameValuePairs.add(new BasicNameValuePair("hash",
            "xxx"));
    nameValuePairs.add(new BasicNameValuePair("OSName", "Android"));
    nameValuePairs.add(new BasicNameValuePair("Model", Build.MODEL));
    nameValuePairs.add(new BasicNameValuePair("Manufacturer",
            Build.MANUFACTURER));
    nameValuePairs.add(new BasicNameValuePair("IMEI", id));
    try {
        nameValuePairs.add(new BasicNameValuePair("SerialNumber",
                Build.SERIAL));
    } catch (NoSuchFieldError e) {
        nameValuePairs.add(new BasicNameValuePair("SerialNumber",
                "NotAvailable"));
    }
    nameValuePairs.add(new BasicNameValuePair("CarrierName", carrier));
    // .add(new BasicNameValuePair("FriendlyName", Build.DEVICE));
    nameValuePairs.add(new BasicNameValuePair("Type", " "));
    nameValuePairs.add(new BasicNameValuePair("VisitorID", VisitorID));
    nameValuePairs.add(new BasicNameValuePair("PhoneNumber", phoneNumber));
    nameValuePairs.add(new BasicNameValuePair("SSID", ssid));
    nameValuePairs.add(new BasicNameValuePair("MACAddress", macAddr));
    nameValuePairs.add(new BasicNameValuePair("UserDepartment",
            department_val));
    nameValuePairs.add(new BasicNameValuePair("BatteryLevel", batteryText));

    int toastDuration = Toast.LENGTH_SHORT;
    try {
        HttpPost httppost = new HttpPost(URL); // post object
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost); // execution
        int result = response.getStatusLine().getStatusCode();
        if (result == 200) {
            CharSequence toastText = "Success";
            Toast.makeText(context, toastText, toastDuration).show();
        } else {
            CharSequence toastText = "Failure";
            Log.d("checkin", String.valueOf(response.getStatusLine()
                    .getStatusCode()));
            Toast.makeText(context, toastText, toastDuration).show();
        }
    } catch (ClientProtocolException e) {
        CharSequence toastText = "ClientProtocolException";
        Toast.makeText(context, toastText, toastDuration).show();
    } catch (IOException e) {
        CharSequence toastText = "IOException";
        Toast.makeText(context, toastText, toastDuration).show();
    }

所以我需要的是一种从httppost中提取完整网址的方法。传递的当前字符串URL只是一个基本网址,例如:http://xx.com/nameValuePairs跟随它。 想法?

1 个答案:

答案 0 :(得分:6)

对于HttpPost,值会作为请求的正文传递,因此网址将与您传入的网址相同。但是,您可以使用EntityUtils.toString(HttpEntity entity) UrlEncodedFormEntity打印出将在HttpPost中传递的值。