Java JSONObjectRequestGet请求在方法返回值后检索值
我正试图通过GET请求检索JsonObject。当我在代码中设置断点时,我看到count()方法没有返回任何内容。之后,调用内部类的onResponse方法并检索所需的值 我在save()方法中调用count()方法。为了创建一个JSONObject。代码在检索正确的客户计数之前创建JSONObject 我正在使用一个名为AppController的自定义Requesque对网络请求进行排队。我希望有人能理解这种奇怪的行为Java JSONObjectRequestGet请求在方法返回值后检索值,java,android,json,rest,Java,Android,Json,Rest,我正试图通过GET请求检索JsonObject。当我在代码中设置断点时,我看到count()方法没有返回任何内容。之后,调用内部类的onResponse方法并检索所需的值 我在save()方法中调用count()方法。为了创建一个JSONObject。代码在检索正确的客户计数之前创建JSONObject 我正在使用一个名为AppController的自定义Requesque对网络请求进行排队。我希望有人能理解这种奇怪的行为 @Override public void save(Custo
@Override
public void save(Customer customer) throws JSONException {
int zw = count();
JSONObject obj = new JSONObject();
obj.put("customernumber", count + 1);
obj.put("name", customer.getName());
obj.put("lastname", customer.getLastname());
obj.put("phonenumber", customer.getPhonenumber());
obj.put("addressid", customer.getAdressID());
obj.put("password", customer.getPassword());
String urlJsonObj = URL;
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
urlJsonObj, obj,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
@Override
public int count() {
String countURL = URL + "/count";
JsonObjectRequest jsonObjReq = new JsonObjectRequest
(Request.Method.GET, countURL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// Parsing json object response
// response will be a json object
count = response.getInt("count");
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d( "Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
return count;
@覆盖
公共void save(客户)抛出JSONException{
int zw=计数();
JSONObject obj=新的JSONObject();
对象放置(“客户编号”,计数+1);
obj.put(“name”,customer.getName());
obj.put(“lastname”,customer.getLastname());
obj.put(“phonenumber”,customer.getPhonenumber());
obj.put(“addressid”,customer.getAddressId());
obj.put(“password”,customer.getPassword());
字符串urlJsonObj=URL;
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Request.Method.POST,
urlJsonObj,obj,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
System.out.println(响应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(“错误:+Error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(JSONOBJEQ);
}
@凌驾
公共整数计数(){
字符串countURL=URL+“/count”;
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest
(Request.Method.GET,countURL,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
//解析json对象响应
//响应将是一个json对象
count=response.getInt(“count”);
}捕获(JSONException e){
e、 printStackTrace();
}
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(“错误:+Error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(JSONOBJEQ);
返回计数;
AppController网络队列
public class AppController extends Application {
public static final String TAG = AppController.class
.getSimpleName();
private RequestQueue mRequestQueue;
private static AppController mInstance;
@Override
public void onCreate() {
super.onCreate();
mInstance = this;
}
public static synchronized AppController getInstance() {
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
mRequestQueue = Volley.newRequestQueue(getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req, String tag) {
// set the default tag if tag is empty
req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
getRequestQueue().add(req);
}
public <T> void addToRequestQueue(Request<T> req) {
req.setTag(TAG);
getRequestQueue().add(req);
}
public void cancelPendingRequests(Object tag) {
if (mRequestQueue != null) {
mRequestQueue.cancelAll(tag);
}
}
公共类AppController扩展应用程序{
公共静态最终字符串标记=AppController.class
.getSimpleName();
私有请求队列mRequestQueue;
专用静态应用控制器;
@凌驾
public void onCreate(){
super.onCreate();
mInstance=这个;
}
公共静态同步AppController getInstance(){
回报率;
}
公共请求队列getRequestQueue(){
if(mRequestQueue==null){
mRequestQueue=Volley.newRequestQueue(getApplicationContext());
}
返回mrequest队列;
}
公共无效addToRequestQueue(请求请求,字符串标记){
//如果标记为空,则设置默认标记
请求setTag(TextUtils.isEmpty(tag)?tag:tag;
getRequestQueue().add(请求);
}
公共无效addToRequestQueue(请求请求){
要求设置标签(标签);
getRequestQueue().add(请求);
}
公共作废取消挂起请求(对象标记){
if(mRequestQueue!=null){
mRequestQueue.cancelAll(标记);
}
}
发生了什么事?
这是由于线程使用不正确造成的。count()
函数在后台线程中执行网络请求,因此当我们从save()
函数调用它时,它不会立即返回计数
解决方案
等待count API的响应,然后执行保存操作
@Override
public void save(Customer customer) throws JSONException {
count();
}
private void performSave(Customer customer, int count) throws JSONException {
int zw = count; // Finally received the count
JSONObject obj = new JSONObject();
obj.put("customernumber", count + 1);
obj.put("name", customer.getName());
obj.put("lastname", customer.getLastname());
obj.put("phonenumber", customer.getPhonenumber());
obj.put("addressid", customer.getAdressID());
obj.put("password", customer.getPassword());
String urlJsonObj = URL;
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
urlJsonObj, obj,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
@Override
public int count(Customer customer) {
String countURL = URL + "/count";
JsonObjectRequest jsonObjReq = new JsonObjectRequest
(Request.Method.GET, countURL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// Parsing json object response
// response will be a json object
count = response.getInt("count");
performSave(customer, count);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d( "Error: " + error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(jsonObjReq);
return 0; // Remove this return type as we will not use it anymore
}
@覆盖
公共void save(客户)抛出JSONException{
计数();
}
私有void performSave(Customer,int count)抛出JSONException{
int zw=count;//最终收到了计数
JSONObject obj=新的JSONObject();
对象放置(“客户编号”,计数+1);
obj.put(“name”,customer.getName());
obj.put(“lastname”,customer.getLastname());
obj.put(“phonenumber”,customer.getPhonenumber());
obj.put(“addressid”,customer.getAddressId());
obj.put(“password”,customer.getPassword());
字符串urlJsonObj=URL;
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest(Request.Method.POST,
urlJsonObj,obj,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
System.out.println(响应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(“错误:+Error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(JSONOBJEQ);
}
@凌驾
公共整数计数(客户){
字符串countURL=URL+“/count”;
JsonObjectRequest JSONObjectReq=新的JsonObjectRequest
(Request.Method.GET,countURL,null,new Response.Listener()){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
//解析json对象响应
//响应将是一个json对象
计数