我正在尝试从使用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);
}
});我知道我正确地连接到了服务器,因为我在服务器的控制台上得到了日志。检索该数据的最简单方法是什么?
发布于 2016-05-17 16:43:07
创建一个包含"person_id“和"age”的模型人物
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();
}
}https://stackoverflow.com/questions/27766624
复制相似问题