我如何可以通过从Android RESTful方法一堆XML而不将其放入网址是什么?(How can

2019-10-20 00:19发布

我知道如何在C#中做到这一点,其实我写了一个提示它在这里 ,但我不知道如何完成它的Java / Android系统。

我的代码是这样的,到目前为止:

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());
    }
}

在Web API方法开始是这样的:

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

正如你所看到的,我传递了两个(小)瓦尔斯在URL(SERIALNUM和siteNum),但我怎么能传递stringifiedXML(这是相当大的,而且看起来比17X麻点劳斯的lockerroomful丑陋如果塞到URL)?

C#的客户端代码使用WebClient的,但我不知道是否有在Java中,以相似的类。

UPDATE

为了响应是Affe(希望他不是德国),并根据我所看到的在这里 :

所以你说什么,而不是我上面,我应该对其进行设置,如下所示:

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));

......那么这样使用UriBuilder:

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");

...使用所有调用服务器:

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();

...用代码来调用的AsyncTask是像这样:

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

更新2

显然不是,作为代码结束了,在到达“ HttpResponse response = httpclient.execute(httppost); ” Logcatting这样的:

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

为什么声称,“ 产生的原因:java.lang.IllegalArgumentException异常:主机名不能为空提供的主机名-为“10.0.2.2:28642” -对吗?

URLToCall最终被“ http://10.0.2.2%3A28642%2Fapi/DeliveryItems/PostArgsAndXMLFileAsStr?serialNum=42&siteNum=some%20Site

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