凌空传球参数的问题

时间:2015-05-13 22:07:55

标签: php android android-volley

(检查variablePost)

好吧,我想通过volley android传递params这是我当前的代码 这是我用volley的代码

Map<String, String> params = new HashMap<String, String>();
    params.put("variablePost", "AndroidVolley");//****
        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            url, new JSONObject(params),
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    Log.d(TAG, response.toString());
                    pDialog.hide();
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d(TAG, "Error: " + error.getMessage());
            pDialog.hide();
        }
    }) {
        @Override
        protected Map<String, String> getParams() throws AuthFailureError {

            Map<String, String> params = new HashMap<String, String>();
            params.put("variablePost", "AndroidVolley");
            return params;
        }
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json; charset=utf-8");
            headers.put("User-agent", "My useragent");
            return headers;
        }
        @Override
        public String getBodyContentType() {
            return "application/json; charset=utf-8";
        }

    };
        jsonObjReq.setRetryPolicy(
            new DefaultRetryPolicy(
                    DefaultRetryPolicy.DEFAULT_TIMEOUT_MS, // 2500
                    DefaultRetryPolicy.DEFAULT_MAX_RETRIES, // 1
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); //1f
 AppController.getInstance().addToRequestQueue(jsonObjReq, "tag_json_obj");

的 我希望变量$ _POST [“variablePost”]通过volley传递,然后将其保存到json然后返回它(对于测试版本) 的

$db = new DB_CONNECT();
$response= array();
$response["dataPerfil"]= array();

mysql_query("SET NAMES 'utf8'");
$sql = sprintf("SELECT p.nombres, p.apellidos, p.tipoSangre, p.email, p.telefono, p.cuentaFace, p.cuentaTwitt, p.cuentaGoogle, p.fondo, p.foto from perfil p left join usuario u on p.idUser= u.id");
$result = mysql_query($sql) or die(mysql_error());
while ($row = mysql_fetch_array($result)) {
$data_perfil = array();
$data_perfil["nombres"]= $row["nombres"];
$data_perfil["variablePost"] = $_POST["variablePost"];//****
$data_perfil["foto"]= $row["foto"];

array_push($response["dataPerfil"], $data_perfil);
}

$response["success"] = 1;
echo json_encode($response);

的 这是logcat的结果 的

wvolley D/MainActivity: {"dataPerfil":[{"variablePost":null,"nombres":"Carolina"},{"variablePost":null,"nombres":"Ricardo"}]}

的 variablePost为null。

3 个答案:

答案 0 :(得分:0)

试试这个:

    StringRequest jsonObjReq = new StringRequest(Method.POST, url, new Response.Listener<String>() {

        @Override
        public void onResponse(String response) {
            ZepUtils.Log(response);
            try {
                JSONObject responseJO = new JSONObject(response);
                //TODO get responseJO variables
            } catch (JSONException e) {
            }
        }

    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            //TODO error();
        }

    }) {

        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("variablePost", "AndroidVolley");
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("User-Agent", "useragent");
            return headers;
        }
    };

    AppController.getInstance().addToRequestQueue(jsonObjReq, "tag_json_obj");

答案 1 :(得分:0)

检查这个

private void post() { 


    final List<NameValuePair> params= new ArrayList<NameValuePair>(); 
    params.add(new BasicNameValuePair("variablexPost","Hello World Android")); 


    String url= getString(R.string.url_test); 
    StringRequest stringRequest = new StringRequest(Request.Method.POST,url,new Response.Listener<String>() 
    { 


        @Override 
        public void onResponse(String response) 
        { 
            Log.d(TAG,"onResponse "+response); 
            Gson gson= new Gson(); 
            ResponseEntity responseEntity= null; 


            try { 
                responseEntity= gson.fromJson(response,ResponseEntity.class); 


                tviResult.setText("success  "+response+"\n"+responseEntity.getData().get(0)); 


            }catch (Exception e) 
            { 


            } 


            vLoading.setVisibility(View.GONE); 


        } 
    },new Response.ErrorListener(){ 


        @Override 
        public void onErrorResponse(VolleyError volleyError) { 
            vLoading.setVisibility(View.GONE); 
        } 
    }){ 


        @Override 
        protected Map<String, String> getParams() throws AuthFailureError
        { 
            Map<String,String> nParams = new HashMap<String, String>();
            for (int i = 0; i <params.size() ; i++)
            { 
                nParams.put(params.get(i).getName(), params.get(i).getValue());
            } 
            Log.d(TAG, "POST params " + nParams.toString());
            return nParams;
        } 


    }; 


    queue.add(stringRequest); 
    vLoading.setVisibility(View.VISIBLE); 
} 

https://github.com/emedinaa/android_volley_php

答案 2 :(得分:0)

试试这个

 private void makeJsonObjReq() {
    Map<String, String> postParam = new HashMap<String, String>();
    postParam.put("email", inputEmail.getText().toString());
    postParam.put("password", inputPassword.getText().toString());


    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            ApiUrl.Login, new JSONObject(postParam),
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {

                    Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show();
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
        }
    }) {

        /**
         * Passing some request headers
         */
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            headers.put("charset", "utf-8");
            return headers;
        }


    };
    mreRequestQueue.add(jsonObjReq);
}

这里mrequestqueue正在处理请求队列