【问题标题】:Hello i am trying to display json data from url into listview. Nothing is displayed though您好我正在尝试将 json 数据从 url 显示到 listview 中。虽然什么都没有显示
【发布时间】:2021-05-21 09:40:37
【问题描述】:

网址

私有静态字符串 JSON_URL ="https://run.mocky.io/v3/aff3f637-d04f-45c0-b002-09be1a143784";

ArrayList<HashMap<String,String>> friendsList;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    friendsList= new ArrayList<>();
    lv = findViewById(R.id.listview);

    GetData getData= new GetData();
    getData.execute();

}

获取数据

公共类 GetData 扩展 AsyncTask{

    @Override
    protected String doInBackground(String... strings) {
        String current = "";

        try {
            URL url;
            HttpURLConnection urlConnection = null;

            try {
                url = new URL(JSON_URL);
                urlConnection = (HttpURLConnection) url.openConnection();


                InputStream in = urlConnection.getInputStream();
                InputStreamReader isr = new InputStreamReader(in);

                int data = isr.read();

                while (data != -1) {

                    current += (char) data;
                    data = isr.read();
                }
                return current;
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (urlConnection != null) {
                    urlConnection.disconnect();
                }
            }
        } catch (Exception e){
            e.printStackTrace();
        }
          return current;
    }

显示数据

    @Override
    protected void onPostExecute(String s) {
        try{
            JSONObject jsonObject= new JSONObject(s);
            JSONArray jsonArray = jsonObject.getJSONArray("Friends"); //name of array

            for(int i = 0; i <jsonArray.length();i++)
            {
                JSONObject jsonObject1 = jsonArray.getJSONObject(i);

                namey=jsonObject.getString("name");
                age= jsonObject1.getString("age");


                //Hashmap

                HashMap<String, String> friends = new HashMap<>();

                friends.put("name",namey);
                friends.put("age", age);

                friendsList.add(friends);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

        //Displaying the results
        ListAdapter adapter = new SimpleAdapter(
                MainActivity.this,
                friendsList,
                R.layout.row_layout,
                new String[]{"name","age"},
                new int[]{R.id.textView, R.id.textView2});

        lv.setAdapter(adapter);

    }
}

}

它没有显示任何内容,我相信它与网址有关,但我不太确定,所以请帮助并提前感谢您

【问题讨论】:

  • String s你检查变量s的值了吗? Toast() 看看。记录一下就知道了。它是空的()吗?您没有检查空()。如果您在 doInbackground(); 中捕获异常,它将为空;
  • 您在浏览器中的网址:SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 26 column 1 of the JSON data
  • catch (JSONException e) { e.printStackTrace(); } 您至少可以在此处显示一个 Toast() 以通知用户。如果有问题,您是否查看了 logcat?
  • 嗨 Atanas - 发帖时请注意格式。另外,请保持您的标题。我尝试编辑以修复代码格式,但无法,因为它警告问题几乎是所有代码。最好在发布之前按照 cmets 中的建议进行基本检查。此外,您可以在帖子中说明您已经尝试做的故障排除:)

标签: java android json


【解决方案1】:

您来自https://run.mocky.io/v3/aff3f637-d04f-45c0-b002-09be1a143784 的模拟 JSON 格式错误,您需要删除最后一个字符

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-06-25
    • 2018-08-08
    • 2018-08-09
    • 2022-11-13
    • 2022-10-16
    • 1970-01-01
    相关资源
    最近更新 更多