Read and upload a JSON file from URL

0

With the Volley library I can load a JSON from a URL and add it to a ListView, the test of the article I saw works well.

This is the code:

public class MainActivity extends AppCompatActivity {
    ListView fruitsList;
    String url = "https://www.thecrazyprogrammer.com/example_data/fruits_array.json";
    ProgressDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fruitsList = (ListView)findViewById(R.id.fruitsList);

    dialog = new ProgressDialog(this);
    dialog.setMessage("Cargando....");
    dialog.show();

    StringRequest request = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String string) {
            parseJsonData(string);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(getApplicationContext(), "Ha ocurrido un error", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });

    RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
    rQueue.add(request);
}

void parseJsonData(String jsonString) {
    try {
        JSONObject object = new JSONObject(jsonString);
        JSONArray fruitsArray = object.getJSONArray("fruits");
        ArrayList al = new ArrayList();

        for(int i = 0; i < fruitsArray.length(); ++i) {
            al.add(fruitsArray.getString(i));
        }

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, al);
        fruitsList.setAdapter(adapter);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    dialog.dismiss();
   }
}

JSON of the test that works:

{
"fruits":[
    "Apple",
    "Apricot",
    "Avocado",
    "Banana",
    "Bilberry",
    "Blackberry",
    "Blackcurrant",
    "Blueberry",
    "Boysenberry",
    "Currant",
    "Cherry",
    "Cherimoya"
    ]
}

The fact is that I only replace the URL and the name of the json array so that it shows a json mine and it no longer works:

My code:

public class MainActivity extends AppCompatActivity {
    ListView fruitsList;
    String url = "http://rodrycode.com.mialias.net/chistesBorrachos.json";
    ProgressDialog dialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    fruitsList = (ListView)findViewById(R.id.fruitsList);

    dialog = new ProgressDialog(this);
    dialog.setMessage("Cargando....");
    dialog.show();

    StringRequest request = new StringRequest(url, new Response.Listener<String>() {
        @Override
        public void onResponse(String string) {
            parseJsonData(string);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError volleyError) {
            Toast.makeText(getApplicationContext(), "Ha ocurrido un error", Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });

    RequestQueue rQueue = Volley.newRequestQueue(MainActivity.this);
    rQueue.add(request);
}

void parseJsonData(String jsonString) {
    try {
        JSONObject object = new JSONObject(jsonString);
        JSONArray fruitsArray = object.getJSONArray("borrachos");
        ArrayList al = new ArrayList();

        for(int i = 0; i < fruitsArray.length(); ++i) {
            al.add(fruitsArray.getString(i));
        }

        ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, al);
        fruitsList.setAdapter(adapter);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    dialog.dismiss();
}

}

And my JSON file:

{
"borrachos":[
    "Prueba de chiste 1 de borrachos",
    "Prueba de chiste 2 de borrachos",
    "Prueba de chiste 3 de borrachos",
    "Prueba de chiste 4 de borrachos",
    "Prueba de chiste 5 de borrachos"
    ]
}

My JSON file I have it in a free test hosting of course (I do not know if it will be server problem.

What the error debug shows:

    
asked by Rodrypaladin 18.11.2018 в 13:01
source

0 answers