如何从http Url获取JSONArray以及如何在android中将JSONArray设置为spinner?

时间:2013-03-11 16:01:38

标签: android android-listview android-spinner arrays

我有JSONArray [“category_1”,“category_2”,“category_3”]而且我没有获取数据。 以及如何在Android中将此数据设置为spinner或listview。

E:

哪里有缺陷?

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;

    }
}    

1 个答案:

答案 0 :(得分:1)

您需要将Json解析为对象。 Here有一些方法可以做到这一点。然后,您需要创建一个ArrayListAdapter并使用arraylist填充它,请参阅here。就是这样。

编辑: 有关创建JsonArray的信息,请参阅此处以获取JSONArray构造函数的字符串。