1

我知道如何在 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)中传递了两个(小)val,但是我如何传递 stringifiedXML(它相当大,如果塞进网址)?

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

更新

作为对 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

4

0 回答 0