0

JSon Data

In this URL, I have some Json Data. How to get that data to my andorid application using that URL.

I have seen references in google. But not getting solution.

I am new to Andorid.

Please help me.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
vishnu
  • 337
  • 4
  • 14

3 Answers3

1

Do this :

Step - 1 Import volley library inyour gradle :

implementation 'com.android.volley:volley:1.1.0'

then in java write this code :

    ProgressDialog progressDialog; // define globally

 public void getLocations(){   //call this method onCreate or on OnClickEvent

    progressDialog = new ProgressDialog(getActivity());
    progressDialog.setMessage("Feteching....");
    progressDialog.setCancelable(false);
    progressDialog.show();

    RequestQueue queue = Volley.newRequestQueue(getApplicationContext());

    StringRequest request = new StringRequest(Request.Method.GET, "YOUR URL", new Response.Listener<String>() { //you can change here POST/GET
        @Override
        public void onResponse(String response) {
            progressDialog.dismiss();
            System.out.println("Response : " + response);
            try {

                 JSONObject jsonResponse = new JSONObject(response);

                  JSONArray locations = jsonResponse.getJSONArray("LOCATIONS");
                  for (int i = 0; i < locations.length(); i++) {
                  JSONObject jsonObject = locations.getJSONObject(i);

               String name = jSONObject.getString("name");
                String lat = jSONObject.getString("lat");
                String lng = jSONObject.getString("lng");
                 System.out.println("LOCATIONS : " + name +"," + lat + "," + lng);

               // check this print in logcats

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


        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            System.out.println("VolloError " + error);
            progressDialog.dismiss();
            Toast.makeText(YourActivity.this, "Network Connection Error...!!!", Toast.LENGTH_SHORT).show();
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();

            // use params when you are using POST method

            return params;
        }

    };

    request.setRetryPolicy(new RetryPolicy() {
        @Override
        public int getCurrentTimeout() {
            return 50000;
        }

        @Override
        public int getCurrentRetryCount() {
            return 50000;
        }

        @Override
        public void retry(VolleyError error) throws VolleyError {

        }
    });
    queue.add(request);
}
Abhinav Gupta
  • 2,225
  • 1
  • 14
  • 30
0

You can Use Retrofit library for parsing JSON data, Retrofit has better performance than volley.

1) Add these dependencies to your app gradle file.

implementation 'com.squareup.retrofit2:retrofit:2.1.0'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'

2) Create a RetrofitInterface for defining annotation like @get or @post.

public interface RetrofitInterface {
@GET("lots.json")
Call<List<Your_pojo_class>> getlocation();
}

3) Create your pojo class

public class location {
private String Name;
private String Lat;
private String Lng;

public String getName() {
    return Name;
}

public void getLat() {
    return Lat;
}
public void getLng() {
    return Lng;
}
}

4) now head back to mainActivity where you'll write your Api service and its on reponse and failure events.

import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class Controller implements Callback<List<Change>> {

static final String BASE_URL = "wsp:88/parking/";

public void start() {
    Gson gson = new GsonBuilder()
            .setLenient()
            .create();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(BASE_URL)
            .addConverterFactory(GsonConverterFactory.create(gson))
            .build();

    RetrofitInterface retro = retrofit.create(RetrofitInterface.class);

    Call<List<location>> call =retro.getlocation();
    call.enqueue(this);

}

@Override
public void onResponse(Call<List<location>> call, Response<List<location>> response) {
    // add your on response activity here
}

@Override
public void onFailure(Call<List<location>> call, Throwable t) {
    // add your on failure activity here

}
}

follow this link for further infromation or for better understanding.enter link description here

-1

You should use a library like Retrofit

Bubu
  • 1,533
  • 14
  • 14