{
"status": 200,
"message": "API executed successfully.",
"data": [{
"data": [{
"shopToken": "NQ2",
"Id": 5,
"UserId": 5,
"ShopName": "test",
"ContactName": "test",
"ContactNumber": "test",
"Address": null,
"IsActive": true,
"OwnerName": "Test"
}],
"recordsFiltered": 1,
"recordsTotal": 1,
"draw": 0,
"pageIndex": 0,
"pageSize": 1
}]
}
How can I parse this JSON in Android?
@Override
protected String doInBackground(Void... voids) {
OkHttpClient client = new OkHttpClient.Builder().connectTimeout(5, TimeUnit.MINUTES)
.readTimeout(5, TimeUnit.MINUTES)
.build();
Request request = new Request.Builder()
.url(Config.GLOPOSNET_URL+"getShops?userToken=NQ2")
.build();
try {
Response response = client.newCall(request).execute();
String res = response.body().string();
try {
JSONObject joa = new JSONObject(res);
int status = joa.getInt("status");
msg = joa.getString("message");
System.out.println("status : "+status);
if (status == 200){
JSONArray infoa = joa.getJSONArray("data");
for (int i=0; i<=infoa.length(); i++){
JSONObject johaa = infoa.getJSONObject(i);
System.out.println("object=== "+johaa.toString());
ModelClassShopList pstShopList = new ModelClassShopList();
pstShopList.getShopToken();
pstShopList.getId(johaa.getInt("Id"));
pstShopList.getUserId(johaa.getString("UserId"));
pstShopList.getShopName(johaa.getString("ShopName"));
pstShopList.getContactName(johaa.getString("ContactName"));
pstShopList.getContactNumber(johaa.getString("ContactNumber"));
pstShopList.getAddress(johaa.getString("Address"));
pstShopList.getOwnerName(johaa.getString("OwnerName"));
String shop_token = pstShopList.getShopToken();
String user_name = pstShopList.getShopName(johaa.getString("UserId"));
String user_type = pstShopList.getContactName(johaa.getString("UserId"));
String user_addres = pstShopList.getContactNumber(johaa.getString("UserId"));
System.out.println("shop token=== "+shop_token);
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences.Editor editor = pref.edit();
editor.putString(Config.SHOP_TOKEN, shop_token);
editor.commit();
}
}else {
AlertDialog.Builder builder;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
builder = new AlertDialog.Builder(MainActivity.this, android.R.style.Theme_Material_Light_Dialog_Alert);
builder.setMessage(""+msg);
builder.setCancelable(false);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
}
} catch (JSONException e) {
e.printStackTrace();
}
return res;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}