0

我不知道是否不允许执行包含 StringEntity 和附加查询参数的 httpclient,或者此代码是否存在根本错误/我对它在做什么的假设?

既然如此,这就是我认为它应该做的/我正在尝试做的事情:

将已传递给方法的参数附加到 URL 中的 args。IOW、serNum 和 siteNum 应该以如下 URL 结尾:

http://10.0.2.2:28642.api/DeliveryItems/PostArgsAndXMLFileAsStr?serNum=Bla&siteNum=Bla

我有一个更大的参数,我想在 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 */ }

更新 2

Sartorial Delimiter 建议:“尝试使用不同的 HTTP 客户端。内置的 HttpURLConnection 或其他。

但是怎么做?

有了这个:

HttpClient httpclient = new HttpURLConnection();

我得到,“错误:HttpURLConnection 是抽象的;不能被实例化

有了这个:

HttpURLConnection httpclient = new DefaultHttpClient();

... httpclient 没有可用的“.execute()”方法;同样适用于:

HttpURLConnection httpclient = new HttpURLConnection();

...所以...???

4

0 回答 0