sbr*_*rds 3 java android http http-post
我有一个很长的URL正在构建使用nameValuePairs,我正在试图弄清楚为什么帖子在某些设备上导致500错误,而在其他设备上获得200.我需要从中提取完整的URL,httppost虽然我理解它应该是以下格式:
http://xx.com/site.asmx?var1=blah?var2=blah
Run Code Online (Sandbox Code Playgroud)
同样的构建适用于手机,而不适用于平板电脑.我已经尝试查看我的代码并捕捉平板电脑可能无法定义此类变量的任何地方,例如:
final TelephonyManager tm = (TelephonyManager) getBaseContext()
.getSystemService(Context.TELEPHONY_SERVICE);
String tmDevice;
try{
tmDevice = "" + tm.getDeviceId().toString();
}
catch (NullPointerException e)
{
tmDevice = "null";
}
Run Code Online (Sandbox Code Playgroud)
以下是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();
}
Run Code Online (Sandbox Code Playgroud)
所以我需要的是从中提取已完成的URL的方法httppost.URL传递的当前字符串只是一个基本URL,例如:http://xx.com/,并nameValuePairs按照它.思考?
因为HttpPost,值将作为请求的主体传递,因此URL将与您传入的URL相同.但是,您可以使用EntityUtils.toString(HttpEntity entity)UrlEncodedFormEntity打印将在其中传递的值HttpPost.
| 归档时间: |
|
| 查看次数: |
3701 次 |
| 最近记录: |