我正在使用Google Volley Api创建一个Android应用程序来处理我的应用程序和我的PHP脚本之间的连接。 出于某种原因,该应用程序不会向我的服务器发送任何数据,也许这里的一些人可以搞清楚吗?
我的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);
}
}
答案 0 :(得分:1)
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。您要么返回params而不是super.getParams(),要么合并两个结果。由于你的父亲只是StringRequest,你可以在这里返回params(StringRequest不会自己添加任何东西)。
答案 1 :(得分:0)
尝试将代码修改为:
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);
}