【问题标题】:In Android how can I post this JSONObject to a server? [closed]在 Android 中,如何将此 JSONObject 发布到服务器? [关闭]
【发布时间】:2015-09-30 01:39:23
【问题描述】:
HashMap<String,String> list = new HashMap();
list.put("uid",String.valueOf(3));
list.put("longitude", "10.13");
list.put("latitude", "20.33");

JSONObject jo = new JSONObject(list);

文档并不完全清楚接下来要做什么。假设服务器 url 是 http://example.com/update.php 并且服务器能够解析 JSON 对象。

我无法使用已弃用的 apache HTTPClient 对象进行发布,因为我使用的是 API 23,并且无法降级 API

【问题讨论】:

标签: android json post


【解决方案1】:

有多个 Http 客户端库可用,例如 Google VolleyRetrofitLoopJ Async。在使用 JSON 格式时,我个人更喜欢 Volley。

要通过 Volley 发布您的 JSON 对象,您可以这样做:

JsonObjectRequest strReq = new JsonObjectRequest(Request.Method.POST,
        YOUR_URL, yourJsonObject, new Response.Listener<JSONObject>() {

    @Override
    public void onResponse(JSONObject jObj) {
        // do these if it request was successful
    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        // do these if it request has errors
    }
});

这些是一些有用的 Google Volley 教程链接:

Android working with Volley Library

An Introduction to Volley

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-27
    • 2014-05-10
    相关资源
    最近更新 更多