【问题标题】:Error while fetching JSON data获取 JSON 数据时出错
【发布时间】:2015-08-29 02:15:36
【问题描述】:

我正在开发一个 android 应用程序。在这里,我想通过发送 locale 表示本地语言来获取国家/地区名称。谁能告诉我问题出在哪里?我无法获取值并显示在微调器中。 我已经这样做了:-

 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);
    }
}

请帮帮我。

【问题讨论】:

  • 你遇到了什么错误?
  • 跳过了 48 帧..我认为微调器和 json 数组有问题..因为我的服务器正在接收请求..
  • @user3553286 发布。
  • protected void onPostExecute(String result) {} 这是问题所在..但我无法理解..

标签: android json spinner


【解决方案1】:

这里的问题是 onPostExecute 方法中的 while 循环。哪个阻塞了 UI 线程并会导致 ANR。你从 System.in 中读到了什么? 根据逻辑,我了解所有国家/地区名称都可以在“结果”对象中使用。检查你在结果对象中得到了什么。 您正在向结果添加值,但未使用它。这意味着 doInBackground 中的逻辑是无用的。我认为您正在尝试通过从输入流中读取结果对象来将其与结果对象分开。取而代之的是,您可以将每一行存储在 ArrayList 中并使用它。下面可能是一个可能的解决方案。希望这会有所帮助!

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);
        }
    }

【讨论】:

  • 从您的程序中我可以获取国家名称。现在如果我想在选择国家名称时获取相应的拨号代码..那么我必须在其中添加“jsonObject.getString("itu") “作为旋转器?我的服务器 json 代码就像 {"name":"Afghanistan","itu":"+93"}
  • 正确 :).. 逻辑类似于“名称”
猜你喜欢
  • 2015-08-17
  • 1970-01-01
  • 1970-01-01
  • 2021-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多