【问题标题】:Get JSON data from LoopJ AndroidAsyncHttp从 LoopJ AndroidAsyncHttp 获取 JSON 数据
【发布时间】:2015-01-04 14:40:56
【问题描述】:

我正在尝试使用 MySQL 数据库从我的 node.js 服务器获取响应。

当我使用浏览器连接到服务器时,我得到如下结果:

[{"person_id":0,"age":18},{"person_id":1,"age":17},{"person_id":2,"age":30}]

我想要做的是在按下按钮后与我的 Android 应用程序获得相同的结果。

我想用 LoopJ AndroidAsyncHttp:

AsyncHttpClient client = new AsyncHttpClient();
client.get("http://localhost:3000", new JsonHttpResponseHandler() {
       @Override
        public void onSuccess(int statusCode, Header[] headers, JSONArray response) {
           super.onSuccess(statusCode, headers, response);
        }
});

我知道我已正确连接到服务器,因为我登录了服务器的控制台。 检索该数据的最简单方法是什么?

【问题讨论】:

    标签: android mysql json android-async-http loopj


    【解决方案1】:

    创建一个包含“person_id”和“age”的模型Person

    AsyncHttpClient client = new AsyncHttpClient();
            client.get("http://localhost:3000",new AsyncHttpResponseHandler() {
                @Override
                public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                    try {
                        String response = responseBody == null ? null : new String(
                                responseBody, getCharset());
                        Log.d("Response: ", response);
                        Gson gson = new Gson();
                        Person[] arr = gson.fromJson(response, Person[].class);
    
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
    

    【讨论】:

      猜你喜欢
      • 2012-12-25
      • 1970-01-01
      • 2015-01-26
      • 1970-01-01
      • 2013-07-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多