Volley library and JSON parsing with Java Android

Volley library and JSON parsing with Java Android

Hello everyone, in this article we are going to talk about Volley library in android java and how can we communicate with a web service, and after it we will read a JSON data.

Let's get started.

First we have tp add below line to our App build.gradle file dependencies section. This will include the volley library in our project. After adding this line, we will Synchronise the file and now we are ready to use volley library.


dependencies {
    compile 'com.android.volley:volley:1.1.0'
}

We will import below classes to use Volley. You will need these classes.


import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
//Below class will be used for volley image
import com.android.volley.toolbox.NetworkImageView;

Now we will post a request to our server.Below code block will fetch a string which will has data of our content.


               String url = String.format(array_urls.url_get_content_detail + "?content_id=1";
               StringRequest postRequest = new StringRequest(Request.Method.GET, url,
                       new Response.Listener() {
                           @Override
                           public void onResponse(String response){


                           }
                       },
                       new Response.ErrorListener() {
                           @Override
                           public void onErrorResponse(VolleyError error) {
                               error.printStackTrace();
                           }
                       });
               Volley.newRequestQueue(getApplicationContext()).add(postRequest);

Below code block will return below JSON data from my server. We will parse below JSON data to fill our fields.


"result":[
{
     "name":[
          "Tone Language Is Key To Perfect Pitch"
     ],
     "id":[
          "105"
     ],
     "image":[
          "https://thecodeprogram.com/image.jpg"
     ],
     "content":[
          "Mozart, Tchaikovsky, Sinatra and Hendrix – these and many other of the world's most famous musicia"
     ],
     "read":[
          "0"
     ],
     "fav":[
          "0"
     ],
     "read_count":[
          "10"
     ],
     "fav_count":[
          "3"
     ]
}
]

Now let's parse the JSON data and fill related fields to show the user. In this application I will put these values to send another activity. So I will send the parsed JSON values from this activty to another activity.


               String url = String.format(const_values.url_get_content_detail + "?content_id=1";
               StringRequest postRequest = new StringRequest(Request.Method.GET, url,
                       new Response.Listener() {
                           @Override
                           public void onResponse(String response){

                             try {
                                        object[0] = new JSONObject(response);

                                        JSONArray show_cnt = object[0].getJSONArray("result");
                                        cnt_title[0] = show_cnt.getJSONObject(0).getString("title").toString();
                                        cnt_content[0] = show_cnt.getJSONObject(0).getString("content").toString();
                                        cnt_image[0] = show_cnt.getJSONObject(0).getString("image");
                                        cnt_id[0] = show_cnt.getJSONObject(0).getString("id");
                                        cnt_fav[0] = show_cnt.getJSONObject(0).getString("fav");
                                        cnt_read[0] = show_cnt.getJSONObject(0).getString("read");

                                        Intent i = new Intent(ContentActivity.this, ReadActivity.class);
                                        i.putExtra("content_title", cnt_title[0]);
                                        i.putExtra("content_content", cnt_content[0]);
                                        i.putExtra("content_image", cnt_image[0]);
                                        i.putExtra("content_id", cnt_id[0]);
                                        i.putExtra("content_fav", cnt_fav[0]);
                                        i.putExtra("content_read", cnt_read[0]);

                                        startActivity(i);

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

                                        Toast.makeText(ContentActivity.this, getResources().getString(R.string.okudum_server_res_code_2), Toast.LENGTH_SHORT).show();
                                    }

                           }
                       },
                       new Response.ErrorListener() {
                           @Override
                           public void onErrorResponse(VolleyError error) {
                               error.printStackTrace();
                           }
                       });
               Volley.newRequestQueue(getApplicationContext()).add(postRequest);

That is all in this article.

Have a good http web requesting.

Burak Hamdi TUFAN


Tags


Share this Post

Send with Whatsapp

Post a Comment

Success! Your comment sent to post. It will be showed after confirmation.
Error! There was an error sending your comment.

Comments

    There is no comment. Be the owner of first comment...