java - 从 HttpPost Android 中提取 URL

标签 java android http http-post

我有一个使用 nameValuePairs 构建的长 URL,我目前正试图弄清楚为什么该帖子在某些设备上导致 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 紧随其后。 想法?

最佳答案

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

关于java - 从 HttpPost Android 中提取 URL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12043185/

相关文章:

http - Bing API 请求的 Ajax 授权 header 是什么?

java - 如何使用 PhantomJSDriver 执行 AJAX 调用?

java - Fab 按钮 android onClick 导致应用程序崩溃?

python - 当 URL 存在时 urllib2 捕获 404 错误

android - 为什么不同的设备有不同的 Action_mask 值

android - 检测自定义 View 上的长按?

.net - 使用 HttpListener 时如何确保 http 请求是本地的

java - 使用条件 API 删除?

java - 在 .conf 文件中为 spring boot 应用程序访问环境变量

java - 如何使用-XX :+UnlockDiagnosticVMOptions -XX:CompileCommand=print option with JVM HotSpot