Android Volley发送空的POST PHP

时间:2018-07-11 19:11:19

标签: php android post android-volley

我的请求到达了服务器,但是我的$ _POST在服务器端变成空的。我尝试了几种不同的方法,包括没有Volley,但仍然是空的。

这是我的Android代码:

public static void register(Context context){
    String url = "https://cserra.com/gbc";

    //Post parameters
    final String action = "register";
    final String name = text_name.getText().toString();
    final String email = text_email.getText().toString();
    final String facebook_id = "0";
    final String auth_code = "";
    final String os_id = "xxxx";

    RequestQueue queue = Volley.newRequestQueue(context);
    StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            Log.d(TAG,response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG,error.getMessage());
        }
    }) {
        @Override
        public byte[] getBody() throws AuthFailureError {
            HashMap<String, String> params2 = new HashMap<String, String>();
            params2.put("action", action);
            params2.put("name", name);
            params2.put("email", email);
            params2.put("facebook_id", facebook_id);
            params2.put("auth_code", auth_code);
            params2.put("os_id", os_id);
            return new JSONObject(params2).toString().getBytes();
        }

        @Override
        public String getBodyContentType() {
            return "application/json";
        }

        @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");
            return headers;
        }
    };
    queue.add(sr);
}

这是我的PHP代码:

<?php
echo file_get_contents("php://input");
print_r($_POST);

这是我的回复:

D / MainActivity:数组                 (                 )

1 个答案:

答案 0 :(得分:0)

我已经将代码更改为此,现在可以正常工作了。

编辑:注意,我创建了一个队列单例,它也可以修复StringRequest代码。

private void register(Context context){
    String url = "https://cserra.com/gbc/";

    //Post parameters
    final String action = "register";
    final String name = text_name.getText().toString();
    final String email = text_email.getText().toString();
    final String facebook_id = "0";
    final String auth_code = "";
    final String os_id = "xxxx";

    RequestQueue queue = SingletonRequestQueue.getInstance(getApplicationContext()).getRequestQueue();

    VolleyLog.DEBUG = true;
    String uri = BASE_URL;

    JSONObject jsonObject = new JSONObject();
    try {
        jsonObject.put("action", action);
        jsonObject.put("name", name);
        jsonObject.put("email", email);
        jsonObject.put("facebook_id", facebook_id);
        jsonObject.put("auth_code", auth_code);
        jsonObject.put("os_id", os_id);
    } catch (JSONException e) {
        e.printStackTrace();
    }

    JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(uri, jsonObject, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d(TAG,"RESPONSE: "+response.toString());
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d(TAG,"ERROR: "+error.toString());
        }
    });

    queue.add(jsonObjectRequest);
}

单身代码:

package com.christopherserra.myapp;

import android.content.Context;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;

public class SingletonRequestQueue {

    private static SingletonRequestQueue mInstance;
    private Context mContext;
    private RequestQueue mRequestQueue;

    private SingletonRequestQueue(Context context) {
        mContext = context;
        mRequestQueue = getRequestQueue();
    }

    public static synchronized SingletonRequestQueue getInstance(Context context) {
        if (mInstance == null) {
            mInstance = new SingletonRequestQueue(context);
        }
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(mContext);
        }
        return mRequestQueue;
    }
}