0

I am sorry for I am a beginner.

I try to put the value into string name[], int age [], int hand[], but all the value is null.

I don't know which part have problem. Since i learn this from youtube.

I just want to store the data to array.

Thank you

public class main extends AppCompatActivity {
String oName[] = new String []; 
int oAge [] = new int [];
int oHand [] = new int [];

protected void onCreate(Bundle savedInstanceState) {...}

private class DownloadTask extends AsyncTask<String, Integer, String> {
    @Override
    protected String doInBackground (String... values) { 
        InputStream is = null;
        String result = ""; //Get json?
        String line;
        try {
            URL ua = new URL("https://api.myjson.com/bins/r5kim"); //json address
            HttpURLConnection conn = (HttpURLConnection) ua.openConnection();
            conn.setRequestMethod("GET");
            conn.connect();
            is = conn.getInputStream();
            BufferedReader reader = new BufferedReader(new InputStreamReader(is));
            while ((line = reader.readLine()) != null) {
                result += line;
            }
            is.close();
           JSONObject jo = new JSONObject(result); //get json
           JSONArray op = jo.getJSONArray("opponents"); //json array?? parse??
            for(int i = 0; i < op.length(); i++) {
                oName[i] = op.getJSONObject(i).getString("name"); //Get null
                oAge[i] = op.getJSONObject(i).getInt("age"); //Get null
                oHand[i] = op.getJSONObject(i).getInt("hand"); //Get null
                }
            } catch (MalformedURLException e) { //exception
                e.printStackTrace();
            } catch (IOException e) { //exception
                e.printStackTrace();
            } catch (JSONException e) { //exception
                e.printStackTrace();
            }
        return null;
    }
}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

1 Answers1

0

Since you didn't post the json string it is hard to determinate what you are doing wrong.

The best advice I can give you is to check out gson library which is the most common json parser on Android (there are obv more, this is the one I use). how to add reference.

Watching videos, searching on google and also here on SO you will find thousands of "how to use gson posts", but anyway I will give you the fastest "how to" for it.

1. Assume you have a string like this:

{
   "id":1,
   "name":"Little Mouse"
}

2. Create an object matching the one passed by json string

public class MyObject{
    private int id;
    private String name;
    //getters and setters
}

3. Now initialize gson and parse the string to the desired object

//Initialize Gson object with GsonBuilder (you can customize it, you will learn)
Gson gson = new GsonBuilder().create();

//parse your string using an object matching your output
MyObject myObject = gson.fromJson(myJsonString, MyObject.class);

It is pretty simple, but if you need help, ask freely

Post scriptum:

you can obiouvsly parse any kind of class, with also custom parameters and nested classes. You just have to create a model class with the parameters you need written with the same name (or you can explicit the desired name) and with the same property type (int, string, ...)

This is your model:

public class myJsonModel{

    private List<Opponent> opponents;

    //getters and setters
}

public class Opponent{
    private String name;
    private String age;
    private String hand;
    //getters and setters
}

So your result should be

myJsonModel myObject = gson.fromJson(myJsonString, myJsonModel.class);
Pier Giorgio Misley
  • 5,305
  • 4
  • 27
  • 66