我不知道是否禁止执行包含StringEntity和附加查询参数的httpclient,或者这个代码是否存在根本错误/我对它正在做什么的假设?
就是这样,这是我认为它应该做的/我正在尝试做的事情:
将已传递给方法的参数附加到URL中的args。 IOW,serNum和siteNum最终应该在URL中结束:
http://10.0.2.2:28642.api/DeliveryItems/PostArgsAndXMLFileAsStr?serNum=Bla&siteNum=Bla
我有一个更大的arg,我希望在httppost的主体内传递,但不在URL中。这个(stringifiedXML)我试图藏匿在StringEntity中。
以下是整个上下文方法:
私有类PostDeliveryItemTask扩展了AsyncTask {
@Override
protected String doInBackground(String... params) {
String result = "";
String stringifiedXML = params[0];
String serNum = params[1];
String siteNum = params[2];
Uri.Builder builder = new Uri.Builder();
builder.scheme("http").authority("10.0.2.2:28642").appendPath("api").appendPath("DeliveryItems").appendPath("PostArgsAndXMLFileAsStr").
appendQueryParameter("serialNum", serNum).appendQueryParameter("siteNum", siteNum);
String URLToCall = builder.build().toString();
try {
StringEntity se = new StringEntity(stringifiedXML);
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URLToCall);
httppost.setEntity(se);
HttpResponse response = httpclient.execute(httppost); // <= goes pear-shaped
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());
}
if (null != result) return result;
}
所有人似乎都很顺利地走过去,直到我走到这一行:
HttpResponse response = httpclient.execute(httppost);
...橡胶真正遇到硅的地方。那时 - 哇哇!它快死了,像巴斯克维尔猎犬一样消失在以太中,进入雾中。是因为混合和匹配URL args和StringEntity?要么??? “setEntity”行没有抱怨,所以我有点怀疑,但是......可能是什么问题? LogCat没有给我任何错误 - 应用程序只是呱呱叫。
当它“简单地呱呱叫”时,它会在AsyncTask.class中的这两行之间结束:
public AsyncTask() { /* compiled code */ }
public final android.os.AsyncTask.Status getStatus() { /* compiled code */ }
Sartorial Delimiter建议:“尝试使用其他HTTP客户端。无论是内置的HttpURLConnection还是其他一些。”
但是怎么样?
有了这个:
HttpClient httpclient = new HttpURLConnection();
我明白了,“错误:HttpURLConnection是抽象的;无法实例化”
有了这个:
HttpURLConnection httpclient = new DefaultHttpClient();
... httpclient没有“.execute()”方法;同样适用于:
HttpURLConnection httpclient = new HttpURLConnection();
...所以... ???