Java Android截击为什么JsonObjectRequest不调用onResponse
我正试图让我的截击Java Android截击为什么JsonObjectRequest不调用onResponse,java,android,json,android-volley,Java,Android,Json,Android Volley,我正试图让我的截击JsonObjectRequest正常工作。调用getById()meth时,不会调用onResponse()。我已经有一个工作岗位的要求。因此,连接参数是正确的。我在LogCat中没有得到任何错误响应或帮助响应 我创建了一个测试类来隔离getById()方法 公共类测试{ 顾客; 公共客户getById(内部id){ Log.i(“CustomerDAO getById”,“调用getById”); 字符串urlJsonObj=”http://192.168.0.39:300
JsonObjectRequest
正常工作。调用getById()
meth时,不会调用onResponse()
。我已经有一个工作岗位的要求。因此,连接参数是正确的。我在LogCat中没有得到任何错误响应或帮助响应
我创建了一个测试类来隔离getById()
方法
公共类测试{
顾客;
公共客户getById(内部id){
Log.i(“CustomerDAO getById”,“调用getById”);
字符串urlJsonObj=”http://192.168.0.39:3000/Customers/“+id;
JsonObjectRequest=newJSONObjectRequest(request.Method.GET,urlJsonObj,null,new Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
//解析json对象响应
//响应将是一个json对象
customer=新客户(response.getInt(“customernumber”)、response.getString(“name”),
response.getString(“lastname”)、response.getString(“phonenumber”),
response.getInt(“addressid”)、response.getString(“密码”);
}捕获(JSONException e){
Log.e(“JSON异常:,e.toString());
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“getByIderErrorResponse”,error.toString());
}
});
AppController.getInstance().addToRequestQueue(请求);
退货客户;
}
}
这是singletonRequestQueue
类
公共类AppController扩展应用程序{
公共静态最终字符串标记=AppController.class
.getSimpleName();
私有请求队列mRequestQueue;
专用静态应用控制器;
@凌驾
public void onCreate(){
super.onCreate();
mInstance=这个;
}
公共静态同步AppController getInstance(){
回报率;
}
公共请求队列getRequestQueue(){
if(mRequestQueue==null){
mRequestQueue=Volley.newRequestQueue(getApplicationContext());
Log.i(“RequestQueue=,mRequestQueue.toString());
}
返回mrequest队列;
}
}
这就是我调用getById()
方法的方式
Test Test=新测试();
Entities.Customer check=test.getById(1);
您的代码中有一些错误。由于volley是一个异步网络库,因此不能像以前那样从网络返回结果。您正在OnResponse内部初始化Customer
模型类,并从外部返回它。
因此,当您执行此操作时将发生什么
创建请求->添加到请求队列->getById()方法返回nullreturn语句最终不会等到响应到达。你每次都会得到空值。因此,当API返回结果或错误时,您必须实现一个自定义回调 为内部测试类创建接口以处理API响应
interface APICallback {
void onSuccess(Customer customer);
void onError(String error);
}
将这些接口实现与id一起传递给getById()方法
public void getById(int id,APICallback callback) {
....
}
根据结果调用方法
@Override
public void onResponse(JSONObject response) {
try {
// Parsing json object response
// response will be a json object
Customer customer = new Customer(response.getInt("customernumber"), response.getString("name"),
response.getString("lastname"), response.getString("phonenumber"),
response.getInt("addressid"), response.getString("password"));
callback.onSuccess(customer);
} catch (JSONException e) {
Log.e("JSON Exception:", e.toString());
}
}
现在可以按如下方式调用getById()
Test test = new Test();
test.getById(1, new Test.APICallback() {
@Override
public void onSuccess(Customer customer) {
}
@Override
public void onError(String error) {
}
});
下面是响应{“customernumber”:1,“name”:“string”,“lastname”:“string”,“phonenumber”:“string”,“addressid”:1,“password”:“string”}
Test test = new Test();
test.getById(1, new Test.APICallback() {
@Override
public void onSuccess(Customer customer) {
}
@Override
public void onError(String error) {
}
});