JSON配列とJSON要素の2つを解析する必要があります。 retrofit
を使用してJSON要素を解析する方法retrofitを使用してJSONレスポンスを解析する方法
JSON Responce例:ここでは
{
"trip_id": "trip101",
"itinerary": [
{
"day": 1,
"name": "Arrive Srinagar 5200ft"
},
{
"day": 2,
"name": "Proceed for Gulmarg 8530ft. (App. 60 Kms/2 Hrs)"
},
{
"day": 3,
"name": "Proceed to pahalgam on 5700ft. (App. 140 Kms/5 Hrs)"
},
{
"day": 4,
"name": "Free for local Sight seens"
},
{
"day": 5,
"name": "Proceed for Srinagar (App. 97 Kms/3 Hrs)"
},
{
"day": 6,
"name": "Proceed for Day trip to Sonamarg 9200ft.(App. 87 Kms/3 Hrs)"
},
{
"day": 7,
"name": "Srinagar Air port Droping"
}
]
}
は、私のPOJOクラスです:
public class MyPojo{
private String trip_id;
private Itinerary[] itinerary;
public String getTrip_id()
{
return trip_id;
}
public void setTrip_id (String trip_id)
{
this.trip_id = trip_id;
}
public Itinerary[] getItinerary()
{
return itinerary;
}
public void setItinerary (Itinerary[] itinerary)
{
this.itinerary = itinerary;
}
@Override
public String toString()
{
return "ClassPojo [trip_id = "+trip_id+", itinerary = "+itinerary+"]";
}
}
別のクラス:
public class Itinerary{
private String name;
private String day;
public String getName() {
return name;
}
public void setName (String name) {
this.name = name;
}
public String getDay() {
return day;
}
public void setDay (String day) {
this.day = day;
}
@Override
public String toString()
{
return "ClassPojo [name = "+name+", day = "+day+"]";
}
}
アダプタ:
public class DayAdapter extends ArrayAdapter<Itinerary> {
private Context context;
private List<Itinerary> days;
public DayAdapter(Context context, int resource, List<Itinerary> objects) {
super(context, resource, objects);
this.context = context;
this.days = objects;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_day, parent, false);
Itinerary itinerary = days.get(position);
TextView tvDay = (TextView)view.findViewById(R.id.tv_day);
TextView tvName = (TextView)view.findViewById(R.id.tv_name);
tvDay.setText(itinerary.getDay());
tvName.setText(itinerary.getName());
return view;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
public static final String ROOT_URL = "http://demo8000805.mockable.io/v1/";
private ListView lvDays;
List<Itinerary> days;
String id;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lvDays = (ListView) findViewById(R.id.lv_days);
//final ProgressDialog loading = ProgressDialog.show(this, "Fetching Data", "Please wait...", false, false);
RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build();
DayAPI api = adapter.create(DayAPI.class);
api.getDays(new Callback<List<Itinerary>>() {
@Override
public void success(List<Itinerary> itineraries, Response response) {
//loading.dismiss();
days = itineraries;
DayAdapter dayadapter = new DayAdapter(getApplicationContext(),R.layout.item_day,days);
lvDays.setAdapter(dayadapter);
}
@Override
public void failure(RetrofitError error) {
//tvName.setText(error.getMessage());
Toast.makeText(getApplicationContext(),"Failed", Toast.LENGTH_LONG).show();
}
});
}
どのように私はこのコードを使用してJSONレスポンスを解析することができますか?
ので、何があなたに起こりますコード? – Raghunandan