Make an HTTP request with android

后端 未结 12 1092
时光取名叫无心
时光取名叫无心 2020-11-21 06:38

I have searched everywhere but I couldn\'t find my answer, is there a way to make a simple HTTP request? I want to request a PHP page / script on one of my websites but I do

12条回答
  •  不思量自难忘°
    2020-11-21 07:22

    Use Volley as suggested above. Add following into build.gradle (Module: app)

    implementation 'com.android.volley:volley:1.1.1'
    

    Add following into AndroidManifest.xml:

    
    

    And add following to you Activity code:

    public void httpCall(String url) {
    
        RequestQueue queue = Volley.newRequestQueue(this);
    
        StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
                new Response.Listener() {
                    @Override
                    public void onResponse(String response) {
                        // enjoy your response
                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        // enjoy your error status
                    }
        });
    
        queue.add(stringRequest);
    }
    

    It replaces http client and it is very simple.

提交回复
热议问题