Android排球 - 发布请求 - 不在线工作

时间:2015-08-06 04:38:17

标签: android http-post android-volley

我试图通过Volley发出POST请求。它在我的localhost中工作得很好但是当我将它移动到webserver时,响应为空。

Java代码

RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                pd.dismiss();
                Log.d("Response: ", response.toString());
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.d("Error: ", error + "");
            }
        }) {
            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("name", name);
                params.put("phone", phone);
                params.put("email", email);

                Log.d("getParams", params.toString());
                return params;
            }

            @Override
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                params.put("Content-Type", "application/x-www-form-urlencoded");

                Log.d("getHeaders", params.toString());
                return params;
            }
        };
        queue.add(sr);

PHP代码

$name = $this->input->post('name');
$phone = $this->input->post('phone');
$email = $this->input->post('email');
echo "Name: ".$name." Phone: ".$phone." Email:".$email; 

2 个答案:

答案 0 :(得分:1)

我之前遇到过相同的问题,并使用自定义请求队列进行修复。

首先创建自定义请求类 CustomRequest.java

import java.io.UnsupportedEncodingException;
import java.util.Map;    
import org.json.JSONException;
import org.json.JSONObject;    
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

public class CustomRequest extends Request<JSONObject> {

private Listener<JSONObject> listener;
private Map<String, String> params;

public CustomRequest(String url, Map<String, String> params,
        Listener<JSONObject> reponseListener, ErrorListener errorListener) {
    super(Method.GET, url, errorListener);
    this.listener = reponseListener;
    this.params = params;
}

public CustomRequest(int method, String url, Map<String, String> params,
        Listener<JSONObject> reponseListener, ErrorListener errorListener) {
    super(method, url, errorListener);
    this.listener = reponseListener;
    this.params = params;
}

protected Map<String, String> getParams()
        throws com.android.volley.AuthFailureError {
    return params;
};

@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
        return Response.success(new JSONObject(jsonString),
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}

@Override
protected void deliverResponse(JSONObject response) {
    // TODO Auto-generated method stub
    listener.onResponse(response);
}
}

然后我按此执行了请求

Map<String, String> params = new HashMap<String, String>();
params.put("name", name);
params.put("email", phone);
params.put("phone", email);



CustomRequest jsonObjReq = new CustomRequest(
                Method.POST,
                Const.URL_SEND_FEEDBACK,params,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject jsonObject) {

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError volleyError) {

                    }
                }) {
            public Map<String, String> getHeaders() throws AuthFailureError {
                Map<String, String> params = new HashMap<String, String>();
                //params.put("Content-Type", "application/x-www-form-urlencoded"); not used this

                Log.d("getHeaders", params.toString());
                return params;
            }


        };

AppController.getInstance().addToRequestQueue(jsonObjReq);

答案 1 :(得分:0)

尝试向其添加charset=utf-8。经过多次搜索,这对我有用。

信用:http://envyandroid.com/android-volley-empty-parameters/

RequestQueue queue = Volley.newRequestQueue(this);
    StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            pd.dismiss();
            Log.d("Response: ", response.toString());
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Error: ", error + "");
        }
    }) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> params = new HashMap<String, String>();
            params.put("name", name);
            params.put("phone", phone);
            params.put("email", email);

            Log.d("getParams", params.toString());
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            Map<String, String> params = new HashMap<String, String>();
            params.put("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");

            Log.d("getHeaders", params.toString());
            return params;
        }
    };
    queue.add(sr);