簡體   English   中英

在 ZE84E30B9390CDB646DB6DB2C9AB7ZD Studio?

[英]Get JSON from url with Java in Android Studio?

我需要將 URL 解析為 json 用於 Android Studio (java) 中的學校項目。 我在網上找到了一些適用於其他人的代碼,但不適用於我。 這是我使用的代碼(使用包含 json 的 url)

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

關於如何讓它發揮作用的任何想法?

您是否在與 REST API 交談? 如果是這樣,您可以查看 Resttemplate(我將它與 Spring Boot 一起使用)。 這是一個 API 實現,用於使用 API 端點或與它們通信。

在這里您可以找到有關它的更多信息: https://www.baeldung.com/rest-template

使用 volley 不是強制性的 這是一個更簡單的代碼

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

} }

使用 new GetData().execute() 調用 class;

它應該有效,因為它對我有用

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM