简体   繁体   English

如何从http URL获取JSONArray以及如何在Android中将JSONArray设置为Spinner?

[英]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. 我有JSONArray [“ category_1”,“ category_2”,“ category_3”],但我没有数据。 And how I set this data to spinner or listview in Android. 以及如何在Android中将此数据设置为微调框或列表视图。

E: 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;

    }
}    

You will need to parse the Json into objects. 您将需要将Json解析为对象。 Here are some ways to do that. 这里有一些方法可以做到这一点。 Then you will need to create an ArrayListAdapter and populate it with an arraylist, see here . 然后,您将需要创建一个ArrayListAdapter并用arraylist填充它,请参见此处 That's it. 而已。

Edit: For creating a JsonArray instead, see here for giving a string to the JSONArray constructor. 编辑:要创建JsonArray,请参见此处,为JSONArray构造函数提供字符串。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM