I don't know if executing an httpclient that contains both a StringEntity and appended query parameters is disallowed or if there is something fundamentally wrong with this code/my assumption about what it's doing?
That being so, this is what I think it's supposed to do/what I'm trying to do:
Attach parameters that have been passed to the method to args within the URL. IOW, serNum and siteNum should end up in the URL like:
http://10.0.2.2:28642.api/DeliveryItems/PostArgsAndXMLFileAsStr?serNum=Bla&siteNum=Bla
I have a larger arg that I want to pass within the body of the httppost, though - not within the URL. This (stringifiedXML) I'm trying to stash within a StringEntity.
Here is the entire method for context:
private class PostDeliveryItemTask extends AsyncTask {
@Override
protected String doInBackground(String... params) {
String result = "";
String stringifiedXML = params[0];
String serNum = params[1];
String siteNum = params[2];
Uri.Builder builder = new Uri.Builder();
builder.scheme("http").authority("10.0.2.2:28642").appendPath("api").appendPath("DeliveryItems").appendPath("PostArgsAndXMLFileAsStr").
appendQueryParameter("serialNum", serNum).appendQueryParameter("siteNum", siteNum);
String URLToCall = builder.build().toString();
try {
StringEntity se = new StringEntity(stringifiedXML);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URLToCall);
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost); // <= goes pear-shaped
result = response.toString();
} catch (ClientProtocolException e) {
Log.e("ClientProtocolException", e.toString());
} catch (UnsupportedEncodingException uex) {
Log.e("UnsupportedEncodingException", uex.toString());
} catch (IOException iox) {
Log.e("IOException", iox.toString());
}
if (null != result) return result;
}
All seems to go well stepping through it until I get to this line:
HttpResponse response = httpclient.execute(httppost);
...where the rubber really meets the silicon. At that point - whammo! It dies a quick death, disappearing into the ether like a Baskerville Hound into the fog. Is it because of mixing and matching URL args and StringEntity? Or??? The "setEntity" line doesn't complain, so I kind of doubt that, but...what could be the problem? LogCat gives me no errors - the app simply croaks.
UPDATE
When it "simply croaks," it ends up between these two lines in AsyncTask.class:
public AsyncTask() { /* compiled code */ }
public final android.os.AsyncTask.Status getStatus() { /* compiled code */ }
UPDATE 2
Sartorial Delimiter suggested: "Try using a different HTTP client. Either the built-in HttpURLConnection or some other."
But how?
With this:
HttpClient httpclient = new HttpURLConnection();
I get, "error: HttpURLConnection is abstract; cannot be instantiated"
With this:
HttpURLConnection httpclient = new DefaultHttpClient();
...there is no ".execute()" method available for httpclient; and the same goes for:
HttpURLConnection httpclient = new HttpURLConnection();
...so...???