//这里我们从setOnClickListener方法调用一个方法调用jsoneParse
private void jsonParse(){
//在这里我插入了我的Url
String url = "https://api.myjson.com/bins/hrtn6";
//json对象请求
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
//这里是解析json object JSONArray jsonArray =response.getJSONArray(“students”)的重要位置;
for(int i = 0; i < jsonArray.length();i++){
JSONObject student = jsonArray.getJSONObject(i);
String firstname = student.getString("firstname");
String lastname = student.getString("lastname");
int age = student.getInt("age");
detailsText.append(firstname+" "+lastname+" "+String.valueOf(age)+"\n");
}
}
//这些都是那些错误处理
catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
//这里我们将请求插入到请求队列中
mQueue.add(request);
}
发布于 2018-06-21 14:40:16
只需将request method
更改为get
,如下所示
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
.
.
.
发布于 2018-06-21 14:43:15
改变这一点
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, null, new Response.Listener<JSONObject>() {
要这样做:
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
发布于 2018-06-21 15:09:08
将requestbody设置为string而不是null (url后的参数)
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, "", new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
.....
}
}
这对我很有帮助,希望这能帮助你,快乐的编码
https://stackoverflow.com/questions/50959426
复制相似问题