0
在我的項目中,如果我用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);
}
});
}
}
對於您需要接受請求作爲POST方法,你需要改變你的要求的方法method.POST – Vickyexpert