【发布时间】:2017-02-01 21:49:37
【问题描述】:
我正在使用 Google Volley Api 创建一个 android 应用程序,用于处理我的应用程序和我的 php 脚本之间的连接。 出于某种原因,该应用程序没有向我的服务器发送任何数据,也许这里有人可以解决?
我的getParams() 方法调用正确,但是当我尝试打印它时,它返回null!一切实际上都在工作,除了我相信数据没有正确传递,因为我的 php 脚本抱怨变量为空
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);
}
}
【问题讨论】:
-
在原始问题中放入重要的信息,例如 getParams。没有它,听起来您的请求队列没有运行,这意味着您未能调用 start()。
-
我调用了一个单例类,我相信它完全正确。但是,我的 php 服务器端脚本获取空(null)值,getparams.put 方法应该阻止吗?他们不应该吗?
-
加布有什么想法吗??
-
这个问题在volley post issues解决了
标签: java php android android-volley