【问题标题】:Create a HttpEntity创建一个 HttpEntity
【发布时间】:2015-12-13 13:27:36
【问题描述】:

我正在尝试更新到 com.loopj.android:android-async-http 的最新版本 (1.4.9),其中 org.apache.http 已替换为 cz.msebera.android.httpclient

目前我使用:

StringEntity entity = new StringEntity("some data");
client.post(static_context, getAbsoluteUrl(url), entity, "application/json", responseHandler);

所以我认为我可以将其转换为 HttpEntity,但事实并非如此。

Caused by: java.lang.ClassCastException: org.apache.http.entity.StringEntity cannot be cast to cz.msebera.android.httpclient.HttpEntity

所以我的问题是如何使用我的数据创建HttpEntity,或者有没有更好的方法来创建带有正文中数据的发布请求?

【问题讨论】:

    标签: java android android-async-http loopj


    【解决方案1】:

    您似乎导入了错误的 StringEntity 类。

    假设cz.msebera.android.httpclient还有StringEntity,你应该可以

    import cz.msebera.android.httpclient.entity.StringEntity
    

    而不是

    import org.apache.http.entity.StringEntity
    

    【讨论】:

    • 好吧,我以为是这样,但事实并非如此。
    • 嗯.. 在某些时候会使用 org.apache.http.* 类,否则您将不会得到这个 ClassCastException。
    • 啊,你是对的,StringEntity 是org.apache.http.entity.StringEntity,但我在cz.msebera.android.httpclient. 中没有找到类似的类
    • 啊抱歉,可能是cz.msebera.android.httpclient.entity.StringEntity
    • 我本可以自己想出来的 m)。只是cz.msebera.android.httpclient.entity.StringEntity 而不是cz.msebera.android.httpclient.StringEntity
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-13
    • 2016-11-01
    • 1970-01-01
    • 2016-11-25
    • 2017-12-24
    • 2017-12-24
    • 1970-01-01
    相关资源
    最近更新 更多