【问题标题】:JSON array response in android using okhttpandroid中使用okhttp的JSON数组响应
【发布时间】:2018-12-18 10:11:48
【问题描述】:

我想将我的 Json 数组中的第一个值检索到我的 android 代码中,为此我使用 okhttp。这是我的代码

public class MainActivity extends AppCompatActivity {
private TextView mTextView;



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTextView = findViewById(R.id.textView);
    OkHttpClient client = new OkHttpClient();
    String url = "http://celebshop.freesite.host/wp-json/wp/v2/pages?fields=slug";
    Request request = new Request.Builder().url(url).build();

    client.newCall(request).enqueue(new Callback() {
        @Override
        public void onFailure(Call call, IOException e) {
            e.printStackTrace();
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if(response.isSuccessful()){
                final String [] myResponse = response.body().string();

                MainActivity.this.runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        String [] myJson = myResponse;

                        String firstObj = myJson[0];

                        mTextView.setText(firstObj);
                    }
                });
            }
        }
    });

}
}

我在 final String [] myResponse = response.body().string() 上遇到错误;说需要不兼容的类型 java.lang.String[] found java.lang.String

这是我想要获取的 Json 数据

[
  {
    "slug": "messages"
  },
  {
    "slug": "store"
  },
  {
    "slug": "my-account"
  },
  {
    "slug": "checkout"
  },
  {
   "slug": "cart"
  },
  {
    "slug": "shop"
  },
  {
    "slug": "categories"
  },
  {
    "slug": "home-with-map"
  },
  {
    "slug": "contact"
  },
  {
    "slug": "blog"
  }
]

【问题讨论】:

  • 想想错误的含义...阅读 API 文档...body().string() 总是返回一个字符串,而不是一个数组
  • FWIW,如果你想解析 JSON,Retrofit 可能是一个比 OkHttp 更好的库

标签: android json okhttp


【解决方案1】:

试试这个(假设你收到一个 JSON 数组):

final JSONArray myResponse = new JSONArray(response.body());

这样您就可以遍历您的响应:

MainActivity.this.runOnUiThread(new Runnable() {
    @Override
    public void run() {
        for ( int i = 0; i < myResponse.length(); i++) {
            // Assuming your JSONArray contains JSONObjects
            JSONObject entry = myResponse.getJSONObject(i);

        }

        // Or simply in your case:
        mTextView.setText(myResponse.getJSONObject(0));
    }
});

【讨论】:

  • 现在说 myResponse 可能没有在 mTextView 所在的最后一行被初始化
【解决方案2】:
try{

JSONArray myResponse = new JSONArray(response.body());

    if(myResponse != null && myResponse.length() > 0){

        for (int i = 0; i < myResponse.length(); i++) 

            JSONObject object = myResponse.getJSONObject(i);

            // get your data from jsonobject

            mTextView.setText(object.getString(0));

        }

    }
} catch (JSONException e) {
  e.printStackTrace();
}

【讨论】:

  • 未处理的异常 org.json.JSONException
  • 抱歉回复晚了,现在试试吧。
猜你喜欢
  • 2015-03-10
  • 2017-06-07
  • 1970-01-01
  • 2017-07-28
  • 2019-04-12
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 2019-08-27
相关资源
最近更新 更多