Benjamin Ares Varga
Benjamin Ares Varga

Reputation: 63

how to get JSONArray from http Url and how to set JSONArray to spinner in android?

I have JSONArray ["category_1","category_2","category_3"] and I don't get data. And how I set this data to spinner or listview in Android.

E:

where are faults?

public class MainActivity extends Activity {

    String urlCat = "http://tvapp.pcrevue.sk/categories.json";

    Spinner spinner;

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

        try {
            JSONArray jsonArray;
            jsonArray = getJSONArrayFromUrl(urlCat);

            final String[] array_spinner = new String[jsonArray.length()];

            //int show_total = jsonArray.length();

            //Toast.makeText(flash_tattoo.this, show_total + "test", Toast.LENGTH_LONG).show();

            for (int i=0; i<jsonArray.length(); i++)
            {
                String styleValue = jsonArray.getJSONArray(0).getString(i);
                array_spinner[i] = styleValue;

            }
            ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, android.R.layout.simple_spinner_item,array_spinner);

            adapter.setDropDownViewResource(R.layout.activity_main);
            spinner.setAdapter(adapter);


        } catch (JSONException e) {
            e.printStackTrace();
        }


    }

    public JSONArray getJSONArrayFromUrl(String url) {
        InputStream is = null;
        JSONArray jObj = null;
        String json = "";
        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(url);

            HttpResponse httpResponse = httpClient.execute(httpGet);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"));
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                //json += line;
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONArray(json);
        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        return jObj;

    }
}    

Upvotes: 0

Views: 6479

Answers (1)

Jim Baca
Jim Baca

Reputation: 6132

You will need to parse the Json into objects. Here are some ways to do that. Then you will need to create an ArrayListAdapter and populate it with an arraylist, see here. That's it.

Edit: For creating a JsonArray instead, see here for giving a string to the JSONArray constructor.

Upvotes: 1

Related Questions