android - Volley String 请求响应(侦听器)错误
问题描述
我正在使用 Volley String GET 方法,我不知道为什么 Response (Listener) 给我错误,而 ErrorListener 也给错误。这是我的代码,请帮我找出错误
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.VolleyError;
import com.android.volley.Response;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
private void getData(){
//Creating a string request
StringRequest stringRequest = new StringRequest(Request.Method.GET,Config.DATA_URL,new Response.Listner<String>() {
@Override
public void onResponse(String response) {
JSONObject j = null;
try {
//Parsing the fetched Json String to JSON Object
j = new JSONObject(response);
//Storing the Array of JSON String to our JSON Array
result = j.getJSONArray(Config.JSON_ARRAY);
//Calling method getStudents to get the students from the JSON Array
getStudents(result);
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
//Creating a request queue
RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
//Adding request to the queue
requestQueue.add(stringRequest);
}
解决方案
试试这个(对我有用);
final RequestQueue requestQueue= Volley.newRequestQueue(MainActivity.this);
StringRequest stringRequest=new StringRequest(Request.Method.POST, serverURL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Do something with response string
tv_respuesta.setText(response);
requestQueue.stop();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Do something when get error
tv_respuesta.setText(error.toString());
requestQueue.stop();
}
}
);
requestQueue.add(stringRequest);
推荐阅读
- java - 这种单例线程的实现是否安全、无序列化问题且性能良好?
- android - 在谷歌云平台上安卓设备(安卓派)和服务器之间的MQTT通信需要一些帮助
- sql-server - “由于“活动事务”,数据库的事务日志已满 - 无法备份、放大、截断或缩小
- r - 如何从文本文件中读取某些行,同时忽略使用 R 之间的几行(也将这些行分隔为列)?
- java - 生产者更快,消费者延迟,Java
- html - 悬停时将 SVG fill="none" 更改为 fill="#FFF"?
- python - 训练神经网络时,Tensorflow 完成后会自动恢复到最佳 epoch 吗?
- php - 如果支持,如何加载一个图像,如果不在 HTML 中,如何加载另一个图像
- postgresql - PGAdmin 使用每个“docker-compose up”创建新的随机卷
- python - 如何使用正则表达式删除字符串中的数字,但主题标签中的数字除外