【问题标题】:Android volley post request not workingAndroid凌空发布请求不起作用
【发布时间】: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


【解决方案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,你可以在这里返回参数(StringRequest不会自己添加任何内容)。

【讨论】:

  • 谢谢,但现在我收到一个巨大的错误,我的应用程序崩溃了!似乎我的 params 方法在崩溃之前被调用了数千次。产生一个巨大的堆栈跟踪,这是第一位:FATAL EXCEPTION: Thread-166 Process: com.example.koenraad.Exigentia, PID: 7288 java.lang.StackOverflowError:
  • 听起来你编码错误并进入了无限递归循环。但是由于您没有发布新代码或完整错误,我无法告诉您如何修复它。但我猜你写的是 return getParams() 而不是 return params;
  • 我真是个白痴。哈哈正确。奇怪的是,我的 php 服务器端仍然抱怨没有收到数据。数据只是空/假。我觉得这很奇怪,因为字符串在我在 .put 方法中使用它们之前就已经被硬编码了。现在 params 包含正确的值,但仍然没有正确发布?
【解决方案2】:

尝试将您的代码修改为:

  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);



    }

【讨论】:

  • 谢谢,但是这样做之后,它仍然没有向我的 php 脚本发送正确的数据。 php 什么也没收到(或 null?)。
  • 好吧,也许我给你我的php mscript,你修改abit
  • 确定吗?哪里可以给?
猜你喜欢
  • 1970-01-01
  • 2017-06-19
  • 1970-01-01
  • 1970-01-01
  • 2016-04-05
  • 2014-08-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多