3

我有一个正在使用的长 URL,nameValuePairs我目前正在尝试弄清楚为什么该帖子在某些设备上导致 500 错误,而在其他设备上却导致 200 错误。我需要从中提取完整的 URL,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传递的字符串只是一个基本 url,例如:http: //xx.com/nameValuePairs跟随它。想法?

4

1 回答 1

6

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

于 2012-08-20T18:56:08.347 回答