从HttpPost Android中提取URL

3

我有一个长的URL,使用nameValuePairs构建,并且我目前正在尝试弄清楚为什么在某些设备上post会导致500错误,而在其他设备上会得到200的结果。 我需要从httppost中提取完整的URL,尽管我理解它应该只是以下格式:

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的方法。当前传递的URL字符串仅是基本URL,例如:http://xx.com/,其后跟着nameValuePairs。您有什么想法?
1个回答

6
对于HttpPost,值将作为请求的主体传递,因此URL将与您传递的URL相同。但是,您可以使用EntityUtils.toString(HttpEntity entity)和UrlEncodedFormEntity一起使用,以打印即将在HttpPost中传递的值。

1
它只显示参数,而不是带参数的完整URL。 - Bachask8
我不知道为什么,但我得到了“”? - user3402040

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接