Val*_*for 8 android android-volley azure-functions
我能够使用Postman和这些参数调用HTTP端点:
{
"name":"Val",
"subject":"Test"
}
Run Code Online (Sandbox Code Playgroud)
但是我无法通过Android对Volley做同样的事情:这里尝试使用JSONRequest:
HashMap<String, String> params2 = new HashMap<String, String>();
params.put("name", "Val");
params.put("subject", "Test Subject");
JsonObjectRequest jsObjRequest = new JsonObjectRequest
(Request.Method.POST, Constants.CLOUD_URL, new JSONObject(params2), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
mView.showMessage("Response: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// TODO Auto-generated method stub
mView.showMessage(error.getMessage());
}
});
// Access the RequestQueue through your singleton class.
VolleySingleton.getInstance(mContext).addToRequestQueue(jsObjRequest);
Run Code Online (Sandbox Code Playgroud)
这里是尝试StringRequest
private void postMessage(Context context, final String name, final String subject ){
RequestQueue queue = Volley.newRequestQueue(context);
StringRequest sr = new StringRequest(Request.Method.POST, Constants.CLOUD_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mView.showMessage(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("name", name);
params.put("subject", subject);
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");
return params;
}
};
queue.add(sr);
}
Run Code Online (Sandbox Code Playgroud)
当我使用JSONRequest时,调用POST但没有参数传递,当我使用StringRequest时,我得到下面的错误?如何将JSON数据传递给Volley电话?
E/Volley: [13053] BasicNetwork.performRequest: Unexpected response code 400 for
Run Code Online (Sandbox Code Playgroud)
这是处理请求的服务器代码
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
var helloRequest = await req.Content.ReadAsAsync<HelloRequest>();
var name = helloRequest?.Name ?? "world";
var responseMessage = $"Hello {personToGreet}!";
log.Info($"Message: {responseMessage}");
return req.CreateResponse(HttpStatusCode.OK, $"All went well.");
}
public class HelloRequest
{
public string Name { get; set; }
public string Subject { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
Nar*_*ren 15
服务器代码期望JSON对象返回字符串或更确切地说是Json字符串.
JsonObjectRequest
JSONRequest在请求主体中发送JSON对象,并期望响应中有JSON对象.由于服务器返回一个字符串,它最终会抛出ParseError
StringRequest
StringRequest发送一个具有正文类型的请求,x-www-form-urlencoded但由于服务器需要一个JSON对象.您最终得到400 Bad Request
解决方案
解决方案是将字符串请求中的内容类型更改为JSON,并在正文中传递JSON对象.因为它已经期待你回复的字符串,所以你很好.代码应如下.
StringRequest sr = new StringRequest(Request.Method.POST, Constants.CLOUD_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
mView.showMessage(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
mView.showMessage(error.getMessage());
}
}) {
@Override
public byte[] getBody() throws AuthFailureError {
HashMap<String, String> params2 = new HashMap<String, String>();
params2.put("name", "Val");
params2.put("subject", "Test Subject");
return new JSONObject(params2).toString().getBytes();
}
@Override
public String getBodyContentType() {
return "application/json";
}
};
Run Code Online (Sandbox Code Playgroud)
此外,服务器代码中还有一个错误
var responseMessage = $"Hello {personToGreet}!";
Run Code Online (Sandbox Code Playgroud)
应该
var responseMessage = $"Hello {name}!";
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
25927 次 |
| 最近记录: |