排球请求未收到

时间:2016-08-11 03:12:57

标签: android android-volley

在我的项目中,如果我使用url添加参数,然后发出服务器正在接收的请求。但是如果我使用GET params方法,则服务器不会收到请求。

成功请求

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText name1=(EditText)findViewById(R.id.editText);
        final EditText price1=(EditText)findViewById(R.id.editText2);
        final EditText description1=(EditText)findViewById(R.id.editText3);

        Button submit=(Button)findViewById(R.id.button);


        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            protected Object clone() throws CloneNotSupportedException {
                return super.clone();
            }

            @Override
            public void onClick(View v) {


                final String name=name1.getText().toString();
                final double price= Double.parseDouble(price1.getText().toString());
                final String description=description1.getText().toString();
                RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
                String url ="http://192.168.0.101/webservice/create_product.php?name=symphony&price=1000&description=from_android";
                StringRequest sr=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jo=new JSONObject(response);
                            Log.d("From Volley",+jo.getInt("success")+"   "+jo.getString("message"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("From Volley", error.getMessage());

                    }
                });
                Log.d("From Volley",sr.getUrl()+"   "+sr.toString());

                queue.add(sr);

            }
        });

    }
}

请求失败

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText name1=(EditText)findViewById(R.id.editText);
        final EditText price1=(EditText)findViewById(R.id.editText2);
        final EditText description1=(EditText)findViewById(R.id.editText3);

        Button submit=(Button)findViewById(R.id.button);


        submit.setOnClickListener(new View.OnClickListener() {
            @Override
            protected Object clone() throws CloneNotSupportedException {
                return super.clone();
            }

            @Override
            public void onClick(View v) {


                final String name=name1.getText().toString();
                final double price= Double.parseDouble(price1.getText().toString());
                final String description=description1.getText().toString();
                RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
                String url ="http://192.168.0.101/webservice/create_product.php";
                StringRequest sr=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        try {
                            JSONObject jo=new JSONObject(response);
                            Log.d("From Volley",+jo.getInt("success")+"   "+jo.getString("message"));
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }


                    }
                }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("From Volley", error.getMessage());

                    }
                }){
                    @Override
                    protected Map<String, String> getParams() throws AuthFailureError {
                        Map<String,String> params = new HashMap<String, String>();
                        params.put("name",name);
                        params.put("price", String.valueOf(price));
                        params.put("description",description);
                        return  params;

                    }

                };

                Log.d("From Volley",sr.getUrl()+"   "+sr.toString());

                queue.add(sr);

            }
        });

    }
}

2 个答案:

答案 0 :(得分:0)

在这里你使用GET方法,你必须在make请求之前构建URL,当请求方法发布时使用getParams() ...

构建GET的URL如下

 Uri.Builder builder = new Uri.Builder();
 builder.scheme("http")
 .authority("192.168.0.101")
 .appendPath("webservice")
 .appendPath("create_product.php")
 .appendQueryParameter("name", name)
 .appendQueryParameter("price",  String.valueOf(price))
 .appendQueryParameter("description", description);

 String url = builder.build().toString();

答案 1 :(得分:0)

 StringRequest sr=new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
 }

更改为

 StringRequest sr=new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
 }