如何添加JSON标头

时间:2016-11-03 07:54:27

标签: android json android-volley

我将private static BufferedReader br; static { try { br = new BufferedReader(new FileReader("pageRefString.txt")); } catch (IOException e) { // Go bang hard - RuntimeException isn't a checked exception throw new RuntimeException(e); } } 发送到服务器的代码......

JSONArray

此代码用于将 StringRequest request= new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() { @Override public void onResponse(String response) { Toast.makeText(getApplicationContext(), " Successfull", Toast.LENGTH_SHORT).show(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { } }){ public Map<String,String> getParams() throws AuthFailureError { Map<String,String> parameters = new HashMap<String, String>(); getResults().put(parameters); return parameters; } }; requestQueue.add(request); } 发送到服务器。如何设置标头,以便服务器可以识别其JSON文件。我该怎么办?

1 个答案:

答案 0 :(得分:3)

设置Content-Type的{​​{1}}标题字段的最简单方法可能是覆盖Request

getBodyContentType()

如果您想在POST请求的正文中发送JSON,则应使用@Override public String getBodyContentType() { return "application/json"; } JsonObjectRequest

例如,如果您的JsonArrayRequest方法返回了您希望作为请求正文发送的getResults(),则可以执行以下操作:

JSONArray