POST方法不使用Params

时间:2015-08-11 11:56:43

标签: java android android-volley

我不知道为什么getParams()在我的方法中不起作用?

System.out.println在getHeaders下运行正常但在getParams下运行不正常吗?

    //---------------------------------------POST request with headers---------------------------------------
public void post(String url, final String param1, final String param2, String source) {

    // Request a string response from the provided URL.
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,
            getReponse(source), createMyReqErrorListener()) {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<>();
            params.put("loginAlias", "username");
            params.put("loginPassword", "12345");
            System.out.println("Params are: " + params.toString());
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put(ACCEPT, ACCEPT_JSON); //Accepting JSON
            headers.put(AUTH_ID, AUTH_ID_VALUE);  
            headers.put(PLATFORM, PLATFORM_VALUE); 
            headers.put(CID, ""); 
            headers.put(DEVICE_TYPE_MOBILE, DEVICE_TYPE_MOBILE_VALUE); 
            System.out.println("Headers are: " + headers.toString());
            return headers;
        }
    };
// Add the request to the RequestQueue.
    queue.add(request);
}

我已经关注了所有Google Volley文档并在SO上尝试了几个选项,但由于某些原因,这不起作用?

由于

1 个答案:

答案 0 :(得分:1)

也面临同样的问题,最后我发现volley有一些问题--JSONObject请求。 (经过几次谷歌搜索!)

getParams()不会调用,因为 JsonObjectRequest 扩展 JsonRequest 会调用 getBody()直接将构造函数第二个参数(call requestBody)编码为contentType,这就是它忽略 getParam()方法的原因。

尝试这个解决方案,它解决了我的问题。

eAX = actxserver('Excel.Application');
mywb = eAX.Workbooks.Open('C:\test.xlsx');
mysheets = eAX.sheets;
numsheets = mysheets.Count;

sheets = cell(1, numsheets);
for ii = 1:numsheets
    sheets{ii} = eAX.Worksheets.Item(ii).Name;
end

mywb.Close(false)
eAX.Quit

在activity / fragment中使用此

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

Volley JsonObjectRequest Post request not working