androidjsongsonjsonobjectrequest

How to get value from json object url in android


I am trying to get url from a json

{
   "file_length":300,
   "versions":[
      {
         "size":38267700,
         "url":"https:\/\/sto006.sh-content.com\/v\/01\/00158\/v4h6zad7kdr1_n\/v4h6zad7kdr1_n.mp4?t=x7xUQpMLNKmr2glDAIN6YetW_hucsU9gzLOq6ifOuWw&s=1677397458&e=129600&f=790153&sp=300&i=0.0",
         "name":"n"
      }
   ],
   "player_img":"https:\/\/sh-content.xyz\/v4h6zad7kdr1_xt.jpg"
}

I want to get only that url from this json file url : https://api.streamhide.com/api/file/direct_link?key=1208rmfu7uutkadgs5rx&file_code=v4h6zad7kdr1

What i tried:

  RequestQueue queue = Volley.newRequestQueue(MainActivity.this);

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Url, null, response -> {
        try {
            String url = response.getString("result");
         Log.d("name",url);

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }, error -> {

        Toast.makeText(MainActivity.this, "Fail to get data..", Toast.LENGTH_SHORT).show();
    });

    queue.add(jsonObjectRequest);

Help me in doing this " getting url object from json file ""url":"https://sto006.sh-content.com"


Solution

  • The solution involve to parse a bit the result variable like this:

    RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
            String Url = "https://api.streamhide.com/api/file/direct_link?key=1208rmfu7uutkadgs5rx&file_code=v4h6zad7kdr1";
            JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, Url, null, response -> {
                try {
    
                    JSONObject responseJSONObject = response.getJSONObject("result");
                    JSONArray array = responseJSONObject.getJSONArray("versions");
                    JSONObject obj = (JSONObject) array.get(0);
                    String url = obj.getString("url");
                    Log.d("name",url);
    
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }, error -> {
    
                Toast.makeText(MainActivity.this, "Fail to get data..", Toast.LENGTH_SHORT).show();
            });
    
            queue.add(jsonObjectRequest);