Tinovimba Mawoyo
Tinovimba Mawoyo

Reputation: 410

JSON array response in android using okhttp

I want to retrieve the first value from my Json array into my android code and i am using okhttp for that.Here is my code

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);
                    }
                });
            }
        }
    });

}
}

I am getting an error on final String [] myResponse = response.body().string(); saying incompitable types required java.lang.String[] found java.lang.String

this is the Json data i want to get

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

Upvotes: 3

Views: 7705

Answers (2)

Kamlesh
Kamlesh

Reputation: 447

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();
}

Upvotes: 1

Prexx
Prexx

Reputation: 2979

Try this (assuming you receive a JSON array):

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

With that you can iterate through your response:

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));
    }
});

Upvotes: 0

Related Questions