我知道如何在C#中执行此操作,事实上我写了一篇关于它的提示here,但我不知道如何在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(相当大,并且看起来比一个更大的更容易看起来更丑陋如果填入URL,则会输出?)
C#客户端代码使用WebClient,但我不知道Java中是否存在类似的类。
回应Affe(希望他不是德国人)并根据我所看到的here:
所以你说的是,而不是我上面所说的,我应该这样设置:
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");
显然不是,因为代码结束了,到达&#34; HttpResponse response = httpclient.execute(httppost);
&#34;记录下来:
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
为什么要声明,&#34; 引起:java.lang.IllegalArgumentException:主机名可能不为空&#34;当提供主机名 时 - 作为&#34; 10.0.2.2:28642&#34; - 对吗?
URLToCall 最终成为&#34; http://10.0.2.2%3A28642%2Fapi/DeliveryItems/PostArgsAndXMLFileAsStr?serialNum=42&siteNum=some%20Site
&#34;