1

I know how to do this in C#, in fact I wrote a tip about it here, but I don't know how to accomplish it in Java/Android.

My code is this so far:

protected void doInBackground(String... params) {
    String URLToCall = "http://10.0.2.2:28642/api/DeliveryItems/PostArgsAndXMLFileAsStr";
    String result = "";
    String stringifiedXML = params[0];
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(URLToCall);
    List<NameValuePair> nvp = new ArrayList<NameValuePair>();
    nvp.add(new BasicNameValuePair("serialNum", "42"));
    nvp.add(new BasicNameValuePair("siteNum", "Some Site"));
    try {
        httppost.setEntity(new UrlEncodedFormEntity(nvp));
        HttpResponse response = httpclient.execute(httppost);
        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());
    }
}

The Web API method starts like this:

[Route("api/DeliveryItems/PostArgsAndXMLFileAsStr")]
public void PostArgsAndXMLFileAsStr([FromBody] string stringifiedXML, string serialNum, string siteNum)
{
    XDocument doc = XDocument.Parse(stringifiedXML);

As you can see, I'm passing two (small) vals in the URL (serialNum and siteNum), but how can I pass stringifiedXML (which is fairly large, and would look 17X uglier than a lockerroomful of pimply louts if stuffed into the URL)?

The C# client code uses a WebClient, but I don't know if there is an analagous class in Java.

UPDATE

In response to Affe (hopefully he's not German) and based on what I see here:

So are you saying that instead of what I have above, I should set it up like so:

String result = "";
String stringifiedXML = params[0];
String serNum = params[1];
String siteNum = params[2];

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URLToCall);
List<NameValuePair> nvp = new ArrayList<NameValuePair>();
nvp.add(new BasicNameValuePair("stringifiedXML", stringifiedXML));

...then use the UriBuilder like this:

Uri.Builder builder = new Uri.Builder();  
builder.scheme("http").authority("10.0.2.2:28642").appendPath("api").appendPath("DeliveryItems").appendPath("PostArgsAndXMLFileAsStr").appendQueryParameter("serialNum", "42").appendQueryParameter("siteNum", "Some Site");

...use all that to call the server:

String URLToCall = builder.build().toString();
try {
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(URLToCall);
    httppost.setEntity(new UrlEncodedFormEntity(nvp));
    HttpResponse response = httpclient.execute(httppost);
    result = response.toString();

...with the code to call the AsyncTask being like so:

_postDeliveryItemTask = new PostDeliveryItemTask();
_postDeliveryItemTask.execute(XMLFileAsStr, "42", "some Site");

?

UPDATE 2

Apparently not, as that code ended up, on reaching "HttpResponse response = httpclient.execute(httppost);" Logcatting this:

04-24 19:35:55.308    1348-1364/hhs.app W/dalvikvm﹕ threadid=12: thread exiting with uncaught exception (group=0xb2b0fba8)
04-24 19:35:55.408    1348-1364/hhs.app E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #2
    Process: hhs.app, PID: 1348
    java.lang.RuntimeException: An error occured while executing doInBackground()
            at android.os.AsyncTask$3.done(AsyncTask.java:300)
            at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
            at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
            at java.util.concurrent.FutureTask.run(FutureTask.java:242)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
     Caused by: java.lang.IllegalArgumentException: Host name may not be null
            at org.apache.http.HttpHost.<init>(HttpHost.java:83)
            at org.apache.http.impl.client.AbstractHttpClient.determineTarget(AbstractHttpClient.java:497)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
            at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
            at hhs.app.RESTfulActivity$PostDeliveryItemTask.doInBackground(RESTfulActivity.java:161)
            at hhs.app.RESTfulActivity$PostDeliveryItemTask.doInBackground(RESTfulActivity.java:131)
            at android.os.AsyncTask$2.call(AsyncTask.java:288)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:841)
04-24 19:35:55.558    1348-1348/hhs.app I/MainActivity﹕ onRestart
04-24 19:35:55.558    1348-1348/hhs.app I/MainActivity﹕ OnStart
04-24 19:35:55.568    1348-1348/hhs.app I/MainActivity﹕ onResume
04-24 19:35:55.718    1348-1348/hhs.app W/EGL_emulation﹕ eglSurfaceAttrib not implemented

Why is it claiming, "Caused by: java.lang.IllegalArgumentException: Host name may not be null" when hostname is provided - as "10.0.2.2:28642" - right?

URLToCall ends up being "http://10.0.2.2%3A28642%2Fapi/DeliveryItems/PostArgsAndXMLFileAsStr?serialNum=42&siteNum=some%20Site"

Community
  • 1
  • 1
B. Clay Shannon-B. Crow Raven
  • 8,547
  • 144
  • 472
  • 862
  • Use a StringEntity instead of a FormEntity? – Affe Apr 24 '14 at 22:30
  • Do you mean StringEntity for the XML, and FormEntity for the other two args? Is that possible? I know (pretty sure) you can't pass more than one arg with the [FromBody] decoration/annotation to Web API... – B. Clay Shannon-B. Crow Raven Apr 24 '14 at 22:41
  • You want both form encoded data and xml in your request body? Or you intended for those two params to be in the query string? You would use a URI builder to generate the URI to get them in the query string, not put them in the request entity. – Affe Apr 24 '14 at 22:57
  • The two args can ride laong on the URL. URIBuilder? I'll have to look that up. – B. Clay Shannon-B. Crow Raven Apr 24 '14 at 23:00
  • Then the approach would be to set the stringified XML content to the body with the HttpEntity and generate a URL that contains the params with the appropriate builder (or just string bash it..) Srry I don't have a full answer, not familiar enough w/ the version of the API used on android to write it out form memory :) . – Affe Apr 24 '14 at 23:07
  • 1
    +1 Just for the Hitchhiker reference – Code-Apprentice Apr 24 '14 at 23:26
  • 1
    If you want to have the data come in as a form parameter named 'stringifiedXML'. To have the body be *just* actual XML, which is usually what you want if it's going to be automatically marshalled into objects in the server use StringEntity – Affe Apr 24 '14 at 23:38
  • @Affe: No, it's not "automatically marshalled into objects in the server" - it is saved as an XML file there. – B. Clay Shannon-B. Crow Raven Apr 25 '14 at 21:32

0 Answers0