0
我試圖使用Volley發出JSON請求,我能夠使用StringRequest成功發出請求,但是現在嘗試執行JSONRequest時發生錯誤。使用Volley的JSON請求
private void postData(final String param, final TextView tv) {
RequestQueue request = Volley.newRequestQueue(this);
JsonObjectRequest postReq = new JsonObjectRequest(Request.Method.GET, url_login, new Response.Listener<JsonReader>() {
@Override
public void onResponse(JsonReader response) {
tv.setText(response); // We set the response data in the TextView
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("Error [" + error + "]");
}
}) {
/**
* Add the headers to the request
* @return headers
* @throws AuthFailureError
*/
@Override
public Map getHeaders() throws AuthFailureError {
Map headers = new HashMap();
headers.put("customHeader", "someCrap");
System.out.println(headers); //testing output of headers
return headers;
}
};
request.add(postReq);
}
我在tv.setText(response)下得到一個錯誤;
無法解析法 '的setText(android.util.JsonReader)'
我想輸出的JSON請求作爲測試在TextView的文本標有 「電視」
謝謝你
有沒有一種方法可以在設備上顯示Json字符串?還是必須解析? – x10sion
我覺得你做的事很奇怪。爲什麼用JsonObjectRequest發起StringRequest?只要執行'StringRequest postReq = new StringRequest(...)'並用String替換'onResponse'中的JsonReader。 – inmyth
對不起,它應該是JsonObjectRequest postReq = new JsonObjectRequest(...)我已經改變了它的問題。關於你的建議,我想要做一個JSON請求作爲一個XML(字符串)請求。我設法從發佈Json的服務器獲得成功的String請求,並且寧願使用Json。我只是在努力通過響應來看到設備上的輸出? – x10sion