本文介绍了出错获取JSON数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个Android app.Here,我想通过发送区域,以获取国家名字的意思是本地language.Can谁能告诉我问题出在哪里?我无法获取在微调的价值和显示器。
我已经这样做了: -

I am developing an android app.Here,I want to fetch the country name by sending locale means local language.Can anyone tell me where is the problem? I can not fetch the value and displays in spinner.I have done this:-

 public class Main2Activity extends Activity {
    final Context context = this;
    public String readJSONFeed(String URL) {
        StringBuilder stringBuilder = new StringBuilder();
        HttpClient client = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(URL);
        try {
            HttpResponse response = client.execute(httpGet);
            StatusLine statusLine = response.getStatusLine();
            int statusCode = statusLine.getStatusCode();
            if (statusCode == 200) {
                HttpEntity entity = response.getEntity();
                InputStream content = entity.getContent();
                BufferedReader reader = new BufferedReader(
                        new InputStreamReader(content));
                String line;
                while ((line = reader.readLine()) != null) {
                    stringBuilder.append(line);
                }} else {
                Log.e("JSON", "Failed to download file");
            }
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return stringBuilder.toString();
    }
    private class ReadJSONFeedTask extends AsyncTask<String, Void, String> {
        protected String doInBackground(String... urls) {
            return readJSONFeed(urls[0]);
        }
        protected void onPostExecute(String result) {
            Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
            try {
            BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
                String line;
             while((line=in.readLine())!=null){
                 JSONArray jsonArray = new JSONArray(line);
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject jsonObject = jsonArray.getJSONObject(i);
                    List<String> list = new ArrayList<String>();list.add(jsonObject.getString("name"));
                    mySpinner
                            .setAdapter(new ArrayAdapter<String>(
                                    Main2Activity.this,
                                    android.R.layout.simple_spinner_dropdown_item,
                                    list));
                }
             }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        String url = context.getResources().getConfiguration().locale
                .getLanguage();
        url = "http://Its my server address?locale=" + url.toUpperCase();
        Log.e("Web call", url);
                new ReadJSONFeedTask().execute(url);
    }
}

请帮我。

推荐答案

这里的问题是在onPostExecute方法while循环。这是阻塞UI线程,将导致ANR。什么是你从System.in读?
按照逻辑,我明白,所有国家名称是结果的对象可用。检查您的结果对象得到什么。
要添加的值以产生,但它不被使用。这意味着在doInBackground逻辑是无用的。我认为你正试图从输入中sTREM阅读它来分隔结果对象行。取而代之的是,你可以存储在ArrayList中的每一行并使用它。以下可以是一个可能的解决方案。希望这有助于!

The problem here is the while loop in onPostExecute method. Which is blocking UI thread and will cause ANR. What are you reading from System.in?As per logic I understand that all Country name is available in "result" object. Check what you are getting in result object. You are adding values to result but it is not used. It means logic in doInBackground is useless. I think you are trying to separate lines from result object by reading it from input strem. Instead of this, you can store each line in ArrayList and use it. Below can be a possible solution. Hope This Helps!

public class Main2Activity extends Activity {
        final Context context = this;

        public ArrayList<String> readJSONFeed(String URL) {
            // Add all lines to this list.
            ArrayList<String> data = new ArrayList<String>();
            HttpClient client = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(URL);
            try {
                HttpResponse response = client.execute(httpGet);
                StatusLine statusLine = response.getStatusLine();
                int statusCode = statusLine.getStatusCode();
                if (statusCode == 200) {
                    HttpEntity entity = response.getEntity();
                    InputStream content = entity.getContent();
                    BufferedReader reader = new BufferedReader(
                            new InputStreamReader(content));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        data.add(line);
                    }
                } else {
                    Log.e("JSON", "Failed to download file");
                }
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return data;
        }

        private class ReadJSONFeedTask extends
                AsyncTask<String, Void, ArrayList<String>> {
            protected ArrayList<String> doInBackground(String... urls) {
                return readJSONFeed(urls[0]);
            }

            protected void onPostExecute(ArrayList<String> result) {
                Spinner mySpinner = (Spinner) findViewById(R.id.spinner1);
                try {
                    // Parse each line and add "name" to this list.
                    List<String> nameList = new ArrayList<String>();
                    for (String line : result) {
                        JSONArray jsonArray = new JSONArray(line);
                        for (int i = 0; i < jsonArray.length(); i++) {
                            JSONObject jsonObject = jsonArray.getJSONObject(i);
                            nameList.add(jsonObject.getString("name"));
                        }
                    }
                    // Once complete traversing is done, set Adapter
                    mySpinner.setAdapter(new ArrayAdapter<String>(
                            Main2Activity.this,
                            android.R.layout.simple_spinner_dropdown_item,
                            nameList));

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

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            String url = context.getResources().getConfiguration().locale
                    .getLanguage();
            url = "http://Its my server address?locale=" + url.toUpperCase();
            Log.e("Web call", url);
            new ReadJSONFeedTask().execute(url);
        }
    }

这篇关于出错获取JSON数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 13:11