请求 JSON 网络服务器

Posted

技术标签:

【中文标题】请求 JSON 网络服务器【英文标题】:Request JSON Web-Server 【发布时间】:2018-04-16 14:43:40 【问题描述】:

我正在做一个 android 应用程序,我正在尝试为 Web 服务器实现 JSON 请求。我正在使用库:android-async-http-1.4.6.jar。 我写了以下代码:

public void RequestToServer() 

    try 

        AsyncHttpClient client = new AsyncHttpClient();
        RequestParams params = new RequestParams();
        // params.put("uid", User_id.toString());
        JSONObject cred = new JSONObject();

        cred.put("user",LoginActivity.m_user);
        cred.put("pwd", LoginActivity.m_pwd);
        Calendar c = Calendar.getInstance();
        cred.put("anno", Integer.toString(c.get(Calendar.YEAR)));

        StringEntity entity = new StringEntity(cred.toString());

        client.post(this, "http://vdctest.agrishare.com/list_up", entity, "application/json",responseHandler);

    catch(Exception e)
    

    

但是我对 client.post 中的语音 responseeHandler 弄错了,我无法弄清楚问题是什么?谁能帮我? 提前感谢您的帮助。

【问题讨论】:

发生了什么或没有发生什么?此外,空的 catch 块也不是一个好主意,因为您只是在默默地捕获可能会告诉您问题的错误 “误以为语音 responseeHandler”.. 这是什么意思?您的响应处理程序是否被调用?您是否认识到您正在调用的 url 正在返回 404? 我无法填写代码,因为我弄错了responseHandler。我不明白是否缺少某些课程。 抱歉,再次不确定:您无法编译和运行您的代码,对吗? 【参考方案1】:

从 cmets 和典型的 google 示例中,我假设您出于某种原因错过了实现 responsehandler 方法。

我建议继续添加一个用于响应处理的内联函数,如此处接受的答案:

android-async-http post request with body

【讨论】:

以上是关于请求 JSON 网络服务器的主要内容,如果未能解决你的问题,请参考以下文章

Android 手机卫士--构建服务端json请求网络数据

iOS开发网络篇—发送json数据给服务器以及多值参数

Python 网络爬虫程序架构及运行流程

如何调试网络请求 Swift

网络请求不工作

C#网络请求与JSON解析