[英]Extract URL from HttpPost Android
我有一個使用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 : nameValuePairs
遵循它。 思考?
對於HttpPost
,值將作為請求的主體傳遞,因此URL將與您傳入的URL相同。但是,您可以將EntityUtils.toString(HttpEntity entity)
與UrlEncodedFormEntity一起打印出來。將在HttpPost
傳遞的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.