how to make a condition to data from json - java

i want to make a condition when i call the json and make it to 'you cannot have the same thing' in the json value when you want to add more data to database.
this is my code
String nim2, ruang2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_kelas);
nim = (EditText) findViewById(R.id.editNim);
ruang = (EditText) findViewById(R.id.ruangPinjam);
new daftarMahasiswa().execute();
}
public void nextButton (View view) {
nim2 = nim.getText().toString();
ruang2 = ruang.getText().toString();
if (ruang2.equals(ruangan)) {
Toast.makeText(KelasActivity.this, "this room has already borrow",Toast.LENGTH_LONG).show();
ruang.setError("you cannot borrow the room again");
}else if (nim2.equals(nimMahasiswa)){
Toast.makeText(KelasActivity.this, "this NIM has borrow the room",Toast.LENGTH_LONG).show();
nim.setError("you cannot borrow it again");
}
class daftarMahasiswa extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(KelasActivity.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
#Override
protected String doInBackground(String... params) {
String link_url = "http://192.168.43.54/datapeminjamankelas/read_mahasiswa.php";
JSONParser jParser = new JSONParser();
JSONObject json = null;
try {
json = jParser.AmbilJson(link_url);
} catch (JSONException e) {
e.printStackTrace();
}
try {
str_json = json.getJSONArray("data");
for (int i = 0; i < str_json.length(); i++) {
JSONObject ar = str_json.getJSONObject(i);
ruangan = ar.getString("ruang").trim();
nimMahasiswa = ar.getString("nim").trim();
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(String s) {
pDialog.dismiss();
}
}
and this is my json
{
"data": [
{
"nim": "103452",
"ruang": "2702"
},
{
"nim": "102341",
"ruang": "2504"
},
{
"nim": "103421",
"ruang": "1101"
}
]
}
what i want to make is the 'nim' and the 'ruang' from json cannot be add again with the same value. it works honestly but only when the data just have 1, when it have more it dont work anymore. please help what can i put to make it right

You can add 'nim' and 'ruang' values in separate List and from there, you can check whether the entered value is already present or not.
List<String> nimList = new ArrayList<>();
List<String> ruangList = new ArrayList<>();
for (int i = 0; i < str_json.length(); i++) {
JSONObject ar = str_json.getJSONObject(i);
ruangan = ar.getString("ruang").trim();
nimMahasiswa = ar.getString("nim").trim();
nimList.add(nimMahasiswa);
ruangList.add(ruangan);
}
You can check like below:
nim2 = nim.getText().toString();
ruang2 = ruang.getText().toString();
if (ruangList.contains(ruang2)) {
Toast.makeText(KelasActivity.this, "this room has already borrow",Toast.LENGTH_LONG).show();
ruang.setError("you cannot borrow the room again");
}else if (nimList.contains(nim2)){
Toast.makeText(KelasActivity.this, "this NIM has borrow the room",Toast.LENGTH_LONG).show();
nim.setError("you cannot borrow it again");
}

Better you could have a pojo class and override equals method like below
public class MyData {
private String myNim;
private String myRuang;
//setter & getters here
#Override
public boolean equals(Object obj) {
if(obj instanceOf MyData)
{
MyData myData = (MyData) obj;
return myData.getMyNim().equals(this.myNim)&&myData.getMyRuang().equals(this.myRuang);
}
return false;
}
}
and now you can check the element exist or not by using below code.
try {
str_json = json.getJSONArray("data");
List<MyData> myDataList = new ArrayList<MyData>();
for (int i = 0; i < str_json.length(); i++) {
JSONObject ar = str_json.getJSONObject(i);
ruangan = ar.getString("ruang").trim();
nimMahasiswa = ar.getString("nim").trim();
MyData myData = new MyData();
myData.setMyNim(nimMahasiswa);
myData.setMyRuang(ruangan);
if(myDataList.contains(myData))
//use your logic here to show error 'you cannot have the same thing'
else
myDataList.add(myData);
}
} catch (JSONException e) {
e.printStackTrace();
}
now you can use the myDataList which is having unique elements.

Related

Don't update realmResult and not set TextView

I have 4 classes. I got some data from the JSON and wrote the realm. After that, in FilmFeature class, I insert some new values to realm but realmResult don't update and TextView comes to null.
Before this class, We have some information on RealmResult but this information not to "Type" or "imdbID"
FilmFeature Class:
JSONArray array1 = jsonObject2.getJSONArray("Search");
for (int i = 0; i < array1.length(); i++) {
final JSONObject film_ozel = array1.getJSONObject(i);
if(baslik_bilgisi.equals(film_ozel.getString("Title"))) {
realm.executeTransactionAsync(new Realm.Transaction() {
#Override
public void execute(Realm realm) {
film film_ozellik = realm.createObject(film.class);
try {
film_ozellik.setType(film_ozel.getString("Type"));
film_ozellik.setImdbID(film_ozel.getString("imdbID"));
realm.insertOrUpdate(film_ozellik);
} catch (JSONException e) {
e.printStackTrace();
}
}
});
}
}
realmResult = realm.where(film.class).findAll();
tür.setText(realmResult.get(position).getType());
imdb.setText(realmResult.get(position).getImdbID());

How to send values from an arraylist from AsyncTask class to another class in Android?

I have the next problem, I tried to send Arraylist<string> values from AsyncTask class to other class call graphics but I don’t know what I do wrong and how to get Arraylist<string> values in the other class, because I have a lot of sintaxis errors I my code
AsyncTask class
public class fetch extends AsyncTask<Void,Void,ArrayList<String>> {
//v funcional
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
protected ArrayList<String> doInBackground(Void... voids) {
String Id ="";
String Time ="";
String Pressure="";
ArrayList<String> IdArray = new ArrayList<String>();
ArrayList<String> TimeArray = new ArrayList<String>();
ArrayList<String> PressureArray = new ArrayList<String>();
String IdS=""+IdArray;
String TimeS=""+TimeArray;
String PresureS=""+PressureArray;
data.set(1,TimeS);
data.set(2,TimeS);
data.set(3,TimeS);
return data;
}
#Override
protected void onPostExecute(ArrayList<String> result){
super.onPostExecute(result);
Graphics.data.setText(data);
}}
The graphics class
public class Graphics extends AppCompatActivity {
public static TextView data;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.graphics);
Button firstButton = (Button) findViewById(R.id.json);
data = (TextView) findViewById(R.id.msgTxt);
firstButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fetch process = new fetch();
ArrayList<String> data= process.execute();
data.get(1);
}
});
}
you can create an interface which your "Graphics" implements it , and pass it to your AsyncTask class.
like this
public interface BlaBlaBla {
void doBla(ArrayList<String> myBla);
}
and in your "Graphics" :
class Graphics extends AppCompatActivity implements BlaBlaBla {
fetch process = new fetch(this);
}
and for asyncClass :
public class fetch extends AsyncTask<Void,Void,ArrayList<String>> {
//constructor
BlaBlaBla bla;
public fetch(BlaBlaBla bla){
this.bla=bla;
}
//when your task is complete use bla.doBla(pass your array here);
}
my solution
Fetch class
public class Fetch extends AsyncTask<Void,Void,String[][]> {
public static int KEY = 0;
#RequiresApi(api = Build.VERSION_CODES.KITKAT)
#Override
protected String[][] doInBackground(Void... voids) {
HttpHandler sh = new HttpHandler();
String url = "http:xxxxxxx/xxx.json";
String jsonStr = sh.makeServiceCall(url);
JSONObject jsonObj = null;
try {
jsonObj = new JSONObject(jsonStr);
int nTiempos=jsonObj.length();
String[] IdKeyArray = new String[nTie];
for (int i = 0; i < jsonObj.length(); i++) {
JSONObject c = jsonObj.getJSONObject(String.valueOf(i));
IdKeyArray[i] = c.getString("Key");
String[][] valores={IdKeyArray};
return valores;
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
}
And this is the "call" the other class where I get the values
private String[][] valores;
Fetch process = new Fetch();
process.execute();
try {
valores=process.get();
} catch (ExecutionException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
pm.setText(String.valueOf(valores[Fetch.Key][0]));
}
}
}

How to save this JSON Object to Array List in Android

I'm having troubles finding a way how to parse this JSONObject on Java:
{"market_cap":[[1518987267000,183700781183],
[1518987567000,183687424480],
[1518987868000,183687424480],
.
.
.
[1519080954726,7730730000]]}
This is the full json: JSON
I´m looking for save these data on a List Array...
I have updated my full function:
static String urlJsonArry2 = "http://coincap.io/history/1day/BTC";
private JSONObject responseJsonObject;
private void GettingDataGraph(){
progressDialog.setMessage("Getting Data");
progressDialog.show();
JsonArrayRequest req = new JsonArrayRequest(urlJsonArry2,
new Response.Listener<JSONArray>() {
#Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
try {
ArrayList<Object> MARKET_CAP_ARRAY_LIST = new ArrayList<>();
JSONArray marketCapArray = responseJsonObject.optJSONArray("volume");
for (int i=0; i< marketCapArray.length(); i++){
JSONArray array1 = (JSONArray) marketCapArray.get(i);
for (int j=0; j < array1.length(); j++){
MARKET_CAP_ARRAY_LIST.add(array1.get(j));
}
}
Log.d(TAG, String.valueOf(MARKET_CAP_ARRAY_LIST));
progressDialog.dismiss();
} catch (JSONException e) {
e.printStackTrace();
Log.d(TAG, "ERROR " + e.getMessage());
progressDialog.dismiss();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
Log.d(TAG, "ERROR " + error.getMessage());
progressDialog.dismiss();
}
});
AppController.getInstance().addToRequestQueue(req);
}
My updated logcat is this:
D/Volley: [1] 5.onErrorResponse: ListMain
D/ListMain: ERROR org.json.JSONException: Value {"volume":[[1518987267000,9151900000],[1518987567000,9157160000],[1518987868000,9169080000],[1518988166000,9161370000],[1518988467000,9154150000],[1518988765000,9144130000],[1518989067000,9128060000],[1518989366000,9117040000],[1518989665000,9113130000],[1518989965000,9094710000],[1518990267000,9110340000],[1518990567000,9144620000],[1518990868000,9168270000],[1518991167000,9179440000],[1518991467000,9181180000],[1518991767000,9175010000],[1518992067000,9175640000],[1518992366000,9161110000],[1518992667000,9140220000],[1518992966000,9146430000],[1518993266000,9164780000],[1518993567000,9136680000],[1518993866000,9087550000],[1518994167000,9035050000],[1518994466000,8991310000],[1518994766000,8990680000],[1518995067000,8973400000],[1518995367000,8941170000],[1518995667000,8895200000],[1518995966000,8853810000],[1518996267000,8821540000],[1518996566000,8778680000],[1518996867000,8778560000],[1518997167000,8802030000],[1518997467000,8808330000],[1518997767000,8793550000],[1518998068000,8778190000],[1518998367000,8744010000],[1518998667000,8730850000],[1518998967000,8739700000],[1518999266000,8708270000],[1518999566000,8692760000],[1518999867000,8696210000],[1519000167000,8691320000],[1519000467000,8704830000],[1519000768000,8701960000],[1519001067000,8703500000],[1519001366000,8690490000],[1519001666000,8654650000],[1519001966000,8645390000],[1519002266000,8642730000],[1519002567000,8594570000],[1519002867000,8634390000],[1519003166000,8671810000],[1519003466000,8673840000],[1519003769000,8680670000],[1519004068000,8676080000],[1519004369000,8672160000],[1519004666000,8635240000],[1519004969000,8601340000],[1519005266000,8610940000],[1519005567000,8609810000],[1519005869000,8623850000],[1519006173000,8627350000],[1519006467000,8593110000],[1519006765000,8582360000],[1519007066000,8558920000],[1519007367000,8520250000],[1519007666000,8534120000],[1519007969000,8565770000],[1519008268000,8568240000],[1519008568000,8568010000],[1519008867000,8551680000],[1519009168000,8520880000],[1519009467000,8505580000],[1519009769000,8499980000],[1519010069000,8460400000],[1519010368000,8443030000],[1519010667000,8435930000],[1519010969000,8385870000],[1519011267000,8336820000],[1519011567000,8314420000],[1519011866000,8306510000],[1519012167000,8289780000],[1519012467000,8265550000],[1519012767000,8230840000],[1519013067000,8198170000],[1519013368000,8184000000],[1519013668000,8183500000],[1519013967000,8177700000],[1519014268000,8173580000],[1519014567000,8169000000],[1519014868000,8156480000],[1519015166000,8142310000],[1519015466000,8135780000],[1519015767000,8146410000],[1519016067000,8142550000],[1519016368000,8121680000],[1519016668000,8100770000],[1519016967000,8107950000],[1519017268000,8058870000],[1519017566000,7990860000],[1519017867000,7953970000],[1519018169000,7918280000],[1519018467000,7881990000],[1519018768000,7823290000],[1519019067000,7815010000],[1519019366000,7808360000],[1519019667000,7784880000],[1519019968000,7749460000],[1519020267000,7735900000],[1519020566000,7734530000],[1519020865000,7718370000],[1519021166000,7699210000],[1519021467000,7685120000],[1519021767000,7677750000],[1519022067000,7660540000],[1519022365000,7627040000],[1519022666000,7621670000],[1519022966000,7620360000],[1519023267000,7613420000],[1519023568000,7583250000],[1519023867000,7563660000],[1519024167000,7581690000],[1519024467000,7592450000],[1519024767000,7575950000],[1519025068000,7623950000],[1519025368000,7705190000],[1519025666000,7750020000],[1519025967000,7794050000],[1519026267000,7820790000],[1519026567000,7814480000],[1519026868000,7819060000],[1519027167000,7811230000],[1519027467000,7812310000],[1519027766000,7806300000],[1519028069000,7717960000],[1519028367000,7717970000],[1519028666000,7739410000],[1519028967000,7770270000],[1519029267000,7787760000],[1519029567000,7778010000],[1519029867000,7753830000],[1519030166000,7743590000],[1519030467000,7744180000],[1519030767000,7712770000],[1519031066000,7684790000],[1519031367000,7648400000],[1519031667000,7630540
I'm getting a onErrorResponse
In case someone is interested...this code works good for this array:
ArrayList<Object> MARKET_CAP_ARRAY_LIST = new ArrayList<>();
try {
JSONObject jsonObject = new JSONObject(s);
JSONArray jsonArray = jsonObject.getJSONArray("market_cap");
for (int i = 0; i < jsonArray.length(); i++) {
JSONArray array1 = (JSONArray) jsonArray.get(i);
for (int j=0; j < array1.length(); j++){
MARKET_CAP_ARRAY_LIST.add(array1.get(j));
Log.d("ARRAY: ", String.valueOf(MARKET_CAP_ARRAY_LIST));
}
}
} catch (JSONException e) {
e.printStackTrace();
}
Thanks...
You can parse JSON like this.
private void parseJsonObject(JSONObject responseJsonObject) throws JSONException {
ArrayList<Object> MARKET_CAP_ARRAY_LIST = new ArrayList<>();
JSONArray marketCapArray = responseJsonObject.optJSONArray("market_cap");
for (int i=0; i< marketCapArray.length(); i++){
JSONArray array1 = (JSONArray) marketCapArray.get(i);
for (int j=0; j < array1.length(); j++){
MARKET_CAP_ARRAY_LIST.add(array1.get(j));
}
}
}
private class JsonAsyncTask extends AsyncTask<Void, Void, Void> {
String url ;
public JsonAsyncTask(String url) {
this.url = url;
}
#Override
protected Void doInBackground(Void... voids) {
processApiResponse(url);
return null;
}
private void processApiResponse(String url) {
try {
URL jsonUrl = new URL(url);
BufferedReader in = new BufferedReader(new InputStreamReader(jsonUrl.openStream()));
String inputLine;
StringBuilder jsonStringBuilder = new StringBuilder();
while ((inputLine = in.readLine()) != null)
jsonStringBuilder.append(inputLine);
in.close();
String jsonString = jsonStringBuilder.toString();
Volume volume = new Gson().fromJson(jsonString, Volume.class);
System.out.println(volume);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
The POJO classes
private class Volume {
ArrayList<VolumeObject> market_cap;
ArrayList<VolumeObject> volume;
}
private class VolumeObject extends ArrayList<Double> {
}
You can use
// https://mvnrepository.com/artifact/com.google.code.gson/gson
compile group: 'com.google.code.gson', name: 'gson', version: '2.7'
to add Gson in your project.
And to call the AsyncTask in an activity
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy);
JsonAsyncTask task = new JsonAsyncTask("http://coincap.io/history/1day/BTC");
task.execute();
}
I hope this helps
Note: For the network operation you can use any other library. Whereas retrofit is preferred by me.

Getting errors when parsing values

I'm getting errors when i'm trying to parse the datas.
The program is to retrieve the datas from database and parse it to the main menu where the RecycleView lists out the datas such as name,username,image and description.
I'm following this tutorial https://www.youtube.com/watch?v=g30Q9KHubTU
This is the code with errors.
DataParser.java
public class DataParser extends AsyncTask<Void,Void,Boolean> { //<<<line 22
Context c;
String jsonData;
RecyclerView rv;
ProgressDialog pd;
ArrayList<Item> items = new ArrayList<>();
public DataParser(Context c, String jsonData, RecyclerView rv) {
this.c = c;
this.jsonData = jsonData;
this.rv = rv;
}
#Override
protected void onPreExecute() {
super.onPreExecute();
pd = new ProgressDialog(c);
pd.setTitle("Parse");
pd.setMessage("Parsing...Please wait.");
pd.show();
}
#Override
protected Boolean doInBackground(Void... params) {
return null;
}
#Override
protected void onPostExecute(Boolean success) {
super.onPostExecute(success);
pd.dismiss();
if(success) //<<<line 59
{
//bind
MyAdapter adapter = new MyAdapter(c,items);
rv.setAdapter(adapter);
}
else
{
Toast.makeText(c, "Unable to parse", Toast.LENGTH_SHORT).show();
}
}
private Boolean parseData()
{
try
{
JSONArray ja = new JSONArray(jsonData);
JSONObject jo;
items.clear();
Item item;
for(int i = 0; i<ja.length();i++)
{
jo=ja.getJSONObject(i);
int itemid = jo.getInt("itemid");
String imagepath = jo.getString("imagepath");
String itemname = jo.getString("itemname");
String username = jo.getString("username");
String itemdesc = jo.getString("itemdesc");
item = new Item();
item.setId(itemid);
item.setItemname(itemname);
item.setUsername(username);
item.setItemdesc(itemdesc);
item.setImagepath(imagepath);
items.add(item);
}
return true;
} catch (JSONException e) {
e.printStackTrace();
}
return false;
}
}
These are the errors
java.lang.NullPointerException:
Attempt to invoke virtual method 'boolean java.lang.Boolean.booleanValue()' on a null object reference
at chengweifeng1132701116.fyp.m_MySQL.DataParser.onPostExecute(DataParser.java:59)
at chengweifeng1132701116.fyp.m_MySQL.DataParser.onPostExecute(DataParser.java:22)
This is because you are always returning null from doInBackground. So, your success variable is always null in onPostExecute(). Have the appropriate logic for parsing inside doInBackground and based on whether it was successful or not, return true or false.

Passing Serializable Objects using AsyncTask

So I am parsing a JSONObject using the JSON Parser, and put that data into several objects using an AsyncTask. My question is: How would I pass all these objects? They are all Serializable and I've read that you can just create an Intent and put the objects into the Intent using the putExtras method; however, when I do this, I get an error.
Also, to create all the objects, I call the constructors of each of the different objects using constructors, in the original constructor. So I can call one constructor, and that constructor call the other constructors, using methods within the first constructor. I know it might sound a little confusing, but ultimately the parser creates 8 different objects that I want to pass to the next activity.
I'm open to any suggestions on how to do this.
EDIT: I don't want to useParcelable it is far to complex for what I am trying to do and I am not worrying about speed as my file is small.
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
checkWifiConnection(menuUrl);
}
class LoadJSON extends AsyncTask<String, Void, Restaurant>{
#Override
protected Restaurant doInBackground(String... params) {
JSONObject jo=null;
Restaurant rest = null;
try {
Log.i(TAG, "Running background thread");
URL mURL = new URL(menuUrl);
Log.i(TAG, mURL + ": menuUrl");
URLConnection uc = mURL.openConnection();
JsonParser parser = new JsonParser();
try {
jo = parser.getJson(getApplicationContext(), uc);
rest = new Restaurant(jo); <<-- Start the parsing using this constructor, which calls the other objects constructors
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return rest;
}
#Override
protected void onPostExecute(Restaurant rest){
ImageButton ib = (ImageButton)findViewById(R.id.continuebutton);
ib.setVisibility(View.VISIBLE);
ib.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent homeIntent = new Intent(MainActivity.this, HomeActivity.class);
homeIntent.putExtra("Restaurant", rest); //<< get an error here
startActivity(homeIntent);
}
});
}
Here is the Restaurant class:
package com.uie.menu.app;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class Restaurant implements Serializable {
private final static String TAG = Restaurant.class.getSimpleName();
String name;
String category;
String created_at;
String updated_at;
private List<Menu> menuList;
private List<SideDishes> sideDishes;
private List<Drinks> drinks;
private List<Ingredients> ingredients;
private List<DrinkCategories> drinkCategories;
private Restaurant() {
menuList = new ArrayList<Menu>();
sideDishes = new ArrayList<SideDishes>();
drinks = new ArrayList<Drinks>();
ingredients = new ArrayList<Ingredients>();
drinkCategories = new ArrayList<DrinkCategories>();
}
public Restaurant(JSONObject jo) throws JSONException {
this();
if (jo != null) {
this.name = jo.optString("name");
Log.i(TAG, name + " : Restaurant 'name'");
this.category = jo.optString("category");
Log.i(TAG, category + " : Restaurant category");
this.created_at = jo.optString("created_at");
Log.i(TAG, created_at + " : Restaurant 'created at'");
this.updated_at = jo.optString("updated_at");
Log.i(TAG, updated_at + " : Restaurant 'updated_at'");
addMenus(jo);
addMenuSides(jo);
addMenuDrinks(jo);
addMenuIngredients(jo);
addDrinkCategories(jo);
}
}
private void addMenus(JSONObject jo) throws JSONException {
JSONArray menus = jo.optJSONArray("menus");
if (menus != null) {
for (int ii = 0; menus != null && ii < menus.length(); ii++) {
JSONObject menu = menus.optJSONObject(ii);
Log.d(TAG, "==Adding Restaurant Menu==");
addMenu(new Menu(menu));
}
Log.d(TAG, "==Done adding Restaurant Menu==");
}
}
private void addMenu(Menu menu) {
menuList.add(menu);
}
private void addMenuSides(JSONObject jo) throws JSONException{
JSONArray sides = jo.optJSONArray("side_dishes");
if (sides != null) {
for (int ii = 0; sides != null && ii < sides.length(); ii++) {
JSONObject side = sides.optJSONObject(ii);
Log.d(TAG, "==Adding Restaurant Side Dish==");
addSide(new SideDishes(side));
}
Log.d(TAG, "==Done adding Restaurant Side Dishes==");
}
}
private void addSide(SideDishes side){
sideDishes.add(side);
}
private void addMenuDrinks(JSONObject jo) throws JSONException{
JSONArray drinks = jo.optJSONArray("drinks");
if (drinks != null) {
for (int ii = 0; drinks != null && ii < drinks.length(); ii++) {
JSONObject drink = drinks.optJSONObject(ii);
Log.d(TAG, "==Adding Restaurant Drinks==");
addDrink(new Drinks(drink));
}
Log.d(TAG, "==Done adding Restaurant Drinks==");
}
}
private void addDrink(Drinks drink){
drinks.add(drink);
}
private void addMenuIngredients(JSONObject jo){
JSONArray ingredients = jo.optJSONArray("ingredients");
if (ingredients != null) {
Log.d(TAG, "==Adding Restaurant Ingredient==");
for (int ii = 0; ingredients != null && ii < ingredients.length(); ii++) {
JSONObject ingredient = ingredients.optJSONObject(ii);
addIngredient(new Ingredients(ingredient));
}
Log.d(TAG, "==Done adding Restaurant Ingredients==");
}
}
private void addIngredient(Ingredients ingredient){
ingredients.add(ingredient);
}
private void addDrinkCategories(JSONObject jo) throws JSONException{
JSONArray drinkCategories = jo.optJSONArray("drink_categories");
if (drinkCategories != null) {
Log.d(TAG, "==Adding Restaurant Drink Categories==");
for (int ii = 0; drinkCategories != null && ii < drinkCategories.length(); ii++) {
JSONObject drinkCategory = drinkCategories.optJSONObject(ii);
addCategory(new DrinkCategories(drinkCategory));
}
Log.d(TAG, "==Done adding Restaurant Drink Categories==");
}
}
private void addCategory(DrinkCategories drinkCategory){
drinkCategories.add(drinkCategory);
}
}
There error that I get is: Cannot refer to a non-final variable rest inside an inner class defined in a different method
Try declaring your rest object in the class scope of LoadJSON, instead of inside doInBackground method. e.g.
class LoadJSON extends AsyncTask<String, Void, Restaurant>{
Restaurant rest = null;
private void doInBackground(...) {
...
}
}
Restaurant should implements Parcelable, it's the best way to pass objects to another activity from Intent.
Edit: You may encounter another bug later, related to you Lists inside the Serializable Restaurant. it's explained here

Categories