[英]Post request working fine in cURL but not in Android Volley
[英]Android volley post request not working
我正在使用Google Volley Api創建一個處理我的應用程序與php腳本之間的連接的android應用程序。 由於某種原因,該應用程序未將任何數據發送到我的服務器,也許這里有人可以找出來?
我的getParams()
方法被正確調用,但是當我嘗試打印它時,它返回null! 我相信一切正常,除了數據傳遞不正確外,我相信,因為我的php腳本抱怨變量為null
public class Server_interaction
{
String server_url = "http://xxxxxx/update_location.php"; //hidden from you ;)
String response_string;
RequestQueue queue;
Context context;
public Server_interaction(Context context)
{
this.context = context;
queue = Server_singleton.getInstance(context).getRequestQueue();
}
public static final String TAG = Server_interaction.class.getSimpleName();
public void post_request(final VolleyCallback callback)
{
StringRequest stringRequest = new StringRequest(Request.Method.POST, server_url,
new Response.Listener<String>()
{
@Override
public void onResponse(String response)
{
response_string = response;
callback.onSuccess(response_string);
//requestQueue.stop();
Log.i(TAG, "the response is: "+ response_string);
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error){
response_string = "Something went wrong";
//error.printstacktrace()
//requestQueue.stop();
Log.i(TAG, "something went wrong. Is the server up and running?");
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
String the_name = "olaf";
String the_mail = "lalalal";
String the_country = "Norway";
String the_latitude = "33";
String the_longitude = "99";
Map<String, String> params = new HashMap<String, String>();
params.put("name", the_name);
params.put("email", the_mail);
params.put("country", the_country);
params.put("latitude", String.valueOf(the_latitude));
params.put("longitude", String.valueOf(the_longitude));
Log.i(TAG, "inside getparams : "+ super.getParams());
return super.getParams();
}
};//stringrequest parameter end
//add request to requestqueue
Server_singleton.getInstance(context).addToRequestQueue(stringRequest);
Log.i(TAG, "the response again:: "+ response_string);
}
}
Map<String, String> params = new HashMap<String, String>();
params.put("name", the_name);
params.put("email", the_mail);
params.put("country", the_country);
params.put("latitude", String.valueOf(the_latitude));
params.put("longitude", String.valueOf(the_longitude));
Log.i(TAG, "inside getparams : "+ super.getParams());
return super.getParams();
您要添加所有參數,然后就忽略它們並調用super.getParams。 您要么返回參數而不是super.getParams(),要么合並兩個結果。 由於您的父母只是StringRequest,因此您可以在此處返回參數(StringRequest不會自己添加任何參數)。
嘗試將您的代碼修改為此:
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL, new Response.Listener<String>() {
@Override
public void onResponse(String s) {
Toast.makeText(ReserveProperty.this, s, Toast.LENGTH_SHORT);
}
}, new Response.ErrorListener(){
@Override
public void onErrorResponse(VolleyError volleyError) {
Toast.makeText(ReserveProperty.this, volleyError.toString(),Toast.LENGTH_LONG).show();
}
}){
protected Map<String,String> getParams()
{
String the_name = "olaf";
String the_mail = "lalalal";
String the_country = "Norway";
String the_latitude = "33";
String the_longitude = "99";
Map<String, String> params = new HashMap<String, String>();
params.put("name", the_name);
params.put("email", the_mail);
params.put("country", the_country);
params.put("latitude", String.valueOf(the_latitude));
params.put("longitude", String.valueOf(the_longitude));
return params;
}
};
Volley.newRequestQueue(this).add(stringRequest);
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.