有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

在Android Studio中使用Java从url获取JSON?

我需要为Android Studio(java)中的一个学校项目解析一个json的URL。我在网上找到了一些对其他人有用的代码,但对我不起作用。以下是我使用的代码(url包含json)

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RequestQueue requestQueue = Volley.newRequestQueue(this);
        JsonArrayRequest jsonArrayRequest = new JsonArrayRequest("http://ineke.broeders.be/2021Android/webservice.aspx?do=getSteden", new Response.Listener<JSONArray>() {
            @SuppressLint("SetTextI18n")
            @Override
            public void onResponse(JSONArray response) {
                TextView textView = findViewById(R.id.result);
                for (int i = 0; i< response.length();i++){
                    try {
                        JSONObject jsonObject = response.getJSONObject(i);
                        int ID = jsonObject.getInt("stadID");
                        String stadNaam = jsonObject.getString("stadNaam");
                        String postcode = jsonObject.getString("postcode");
                        textView.setText(textView.getText() + "Name: " + stadNaam + " postcode: " + postcode);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
        requestQueue.add(jsonArrayRequest);
    }
}

你有什么办法让它发挥作用吗


共 (1) 个答案

  1. # 1 楼答案

    你不必使用截击 下面是一个更简单的代码

    public class GetData extends AsyncTask<String, String, String>{
    
          @Override
          protected String doInBackground(String... strings) {
           String current = "";
         try{
        URL url;
        HttpURLConnection urlConnection = null;
        try{
            url = new URL("http://ineke.broeders.be/2021Android/webservice.aspx?do=getSteden");
            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{
        
        JSONArray jsonArray = new JSONArray(s);
        
            JSONObject jsonObject= jsonArray.getJSONObject(jsonArray);
                       int ID = jsonObject.getInt("stadID");
                        String stadNaam = jsonObject.getString("stadNaam");
                        String postcode = jsonObject.getString("postcode");
                        textView.setText(textView.getText() + "Name: " + stadNaam + " postcode: " + postcode);
                
            
    
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
    

    } }

    使用 新建GetData()。执行()

    它应该有用,因为它对我有用🙃