Extract URL from HttpPost Android

android, http, http-post, java

Solution

For `HttpPost`, the values are passed as the body of the request, so the URL is going to be the same as the one you pass in. You can, however, use `EntityUtils.toString(HttpEntity entity)` with your UrlEncodedFormEntity to print out the values that will be passed in the `HttpPost`.

Problem

I have a long URL that is being built using `nameValuePairs` and I'm currently trying to figure out why the post is resulting in a 500 error on some devices while getting a 200 on other devices. I need to extract the full URL from the `httppost` although as I understand it should just be in the format of: ``` http://xx.com/site.asmx?var1=blah?var2=blah ``` And the same build works on phones while not working on tablets. I've tried looking through my code and catching any places where a tablet may not be able to define such variables like this case: ``` final TelephonyManager tm = (TelephonyManager) getBaseContext() .getSystemService(Context.TELEPHONY_SERVICE); String tmDevice; try{ tmDevice = "" + tm.getDeviceId().toString(); } catch (NullPointerException e) { tmDevice = "null"; } ``` Here is what some of the `nameValuePairs` look like and an excerpt from the posting part of the code: ``` 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(); } ``` So what I need is a way to pull the completed URL from the `httppost`. the current string `URL` being passed is simply a base url such as : http://xx.com/ and the `nameValuePairs` follow it. Thoughts?

Original source