How can I pass a bunch of XML to a RESTful method from Android without putting it into the URL?

笑着哭i 提交于 2019-12-11 05:17:14

问题


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"

来源:https://stackoverflow.com/questions/23280931/how-can-i-pass-a-bunch-of-xml-to-a-restful-method-from-android-without-putting-i

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!