【问题标题】:How to Show Json resluts in Android with fast android networking library如何使用快速的 android 网络库在 Android 中显示 Json 结果
【发布时间】:2018-08-25 03:03:05
【问题描述】:

我正在使用快速 Android 网络库。 Json 响应有效。我在 logcat 中得到了整个 json 字符串。如何在此库的列表视图中的 textview 或 jsonarray 中显示特定的 json 对象或字符串。请帮助,在此先感谢。

    JSONObject json = new JSONObject();

    try {
        json.put("email", loggedInUser);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    Log.e("JSON", json.toString());

    AndroidNetworking.post(Constants.read_profile)
            .addBodyParameter("json", json.toString())
            .setTag("test")
            .setPriority(Priority.MEDIUM)
            .build()
            .getAsJSONObject(new JSONObjectRequestListener() {
                @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
                @Override
                public void onResponse(JSONObject response) {
                    Log.e("READ", response.toString());


                }

                @Override
                public void onError(ANError anError) {
                    Log.e("READ", anError.toString());
                }
            });

【问题讨论】:

  • 你可以使用gson库解析你得到的json,使用模型类,使用gson设置数据到模型类,需要的时候取回来。
  • 粘贴您的 JSON 日志

标签: java android json xml android-layout


【解决方案1】:

尝试使用 GSON 进行 json 解析。我正在您现有的代码中实现 GSON 代码。

在 app gradle 中添加这个 实现 'com.google.code.gson:gson:2.8.1'

对于 JSONObject 响应

getAsJSONObject(new JSONObjectRequestListener() {
    @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
    @Override
    public void onResponse (JSONObject response){
        Log.e("READ", response.toString());

        if (response != null) {
            Gson gson = new Gson();
            Type type = new TypeToken<YourModelClass>() {
            }.getType();
            YourModelClass result = gson.fromJson(response.toString(), type);
        }
    }

    @Override
    public void onError (ANError anError){
        Log.e("READ", anError.toString());
    }
});

对于 JSONArray 响应

@Override
public void onResponse (JSONArray response){
    Log.e("READ", response.toString());

    if (response != null) {
        Gson gson = new Gson();
        Type type = new TypeToken<List<YourModelClass>>() {
        }.getType();
        List<YourModelClass> result = gson.fromJson(response.toString(), type);
    }
}

【讨论】:

    猜你喜欢
    • 2013-01-01
    • 2019-05-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-04-01
    • 2016-12-02
    • 2015-09-21
    相关资源
    最近更新 更多