Send data to server with Volley Lib in Android

点点圈 提交于 2019-12-12 02:57:15

问题


I've developed a Application that i'm going to send data from the phone to the server by Json, to do this i use Volley Library in Android.
but i can not send data to the server!

my simple php code :

$name = $_GET["name"];
$j = array('name' =>$name);
echo json_encode($j);

my java code :

private void makeJsonObjectRequest() {
        mRequestQueue = Volley.newRequestQueue(this);
        String url = "http://my-site-name/sampleGET.php";

        StringRequest jsonObjReq = new StringRequest(Request.Method.GET, url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("result:", response);
                        Toast.makeText(getApplicationContext(), response, Toast.LENGTH_SHORT).show();
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("err", "Error: " + error.getMessage());
                makeJsonObjectRequest();
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<>();
                params.put("name", "json");
                return params;
            }
        };
        mRequestQueue.add(jsonObjReq);
    }

It also got help from the link below : Click to see link

But still could not use it and send data from mobile to server!!!
How can I do this? Please give me correct and how to use it


回答1:


I did a gist on this a while back I hope it can help. You won't me uploading images, however you will be sending an example of which you can see on this line:

entityBuilder.addTextBody("someparameter", params.toString());

In the gist I use apache httpclient 4 with Volley. Link to the gist here.



来源:https://stackoverflow.com/questions/31046259/send-data-to-server-with-volley-lib-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!