我在方法(函数)中使用JsonArrayRequest,返回number。我在需要时使用函数JsonArrayRequest。
但是有些问题:函数返回错误的数字,在函数下面的函数必须返回变量lastNumber,即变量i的最后一个数字。对不起,如果我的qustion很愚蠢,那我是java和android的新手。
注意,函数在第二次使用时返回lastNumber。我建议第一次该程序不实现JsonArrayRequest。
我遵循本教程http://www.androidhive.info/2014/05/android-working-with-volley-library-1/。任何帮助都要感谢!
public int lastNumber = 0;
private int makeJsonObjectRequest(int startList,int endList) {
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
i = startLsit;
while(i < endList) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
/***** TONS OF CODE *****/
movieList.add(movie);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
i++;
lastNumber = i;
}
mAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
AppController.getInstance().addToRequestQueue(movieReq);
return lastNumber
}
答案 0 :(得分:1)
你需要以下面的方式:
添加从服务器获取服务器响应的方法:
public void makeWebServiceRequest(String tag, final String url, int method, JSONObject jPassedObject, final VolleyResponseListener listener){
JsonObjectRequest jsObjRequest = new JsonObjectRequest(method,url,jPassedObject,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
CustomProgressbar.hideProgressBar();
}catch (Exception e){
e.printStackTrace();
}
listener.onVolleySuccess(url, response.toString());
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
NetworkResponse networkResponse = error.networkResponse;
if (networkResponse != null && networkResponse.statusCode == HttpStatus.SC_UNAUTHORIZED) {
// HTTP Status Code: 401 Unauthorized
}
CustomProgressbar.hideProgressBar();
}catch (Exception e){
e.printStackTrace();
}
listener.onVolleyFailure(url);
}
});
AppController.getInstance().addToRequestQueue(movieReq);
}
添加interfece以将数据传递到任何Activity / Fragment:
public interface VolleyResponseListener {
void onVolleySuccess(String url, String serverResponse);
void onVolleyFailure(String url);
}
如何调用此方法?
makeWebServiceRequest("Get Request", "URL HERE", Request.Method.GET, null, new VolleyResponseListener() {
@Override
public void onVolleySuccess(String url, String serverResponse) {
}
@Override
public void onVolleyFailure(String url) {
}
});
注意:您应该在Activity / Fragment中编写逻辑,而不是常见的Web服务调用代码。
您可以使用onVolleySuccess
覆盖方法获取服务器响应。
答案 1 :(得分:1)
您想使用回调接口:
public interface VolleyCallback{
void onSuccess(int result);
}
// Method:
public void getResult(VolleyCallback callback) {
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
i = startLsit;
while(i < endList) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
/***** TONS OF CODE *****/
movieList.add(movie);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
i++;
lastNumber = i;
}
mAdapter.notifyDataSetChanged();
callback.onSuccess(lastNumber);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
callback.onSuccess(lastNumber);
}
});
}}
活动中的示例代码:
public void onResume(){
super.onResume();
getResult(new VolleyCallback(){
@Override
public void onSuccess(int result){
... //do stuff here result is your lastNumber
}
});
}
我希望它有效。
答案 2 :(得分:1)
你使用不正确的方法。在网络回复后你必须返回lastNumber。所以我建议你定义你的函数输出'void'。然后,您必须使用Interface for network。我通过代码解释它。如下所示:
这是你的界面:
public interface JsonObjectListener {
public void onDone(int lastNumber);
public void onError(String error);
}
在您的函数中执行:
private void makeJsonObjectRequest(int startList, int endList, JsonObjectListener listener) {
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
i = startLsit;
while(i < endList) {
try {
JSONObject obj = response.getJSONObject(i);
Movie movie = new Movie();
/***** TONS OF CODE *****/
movieList.add(movie);
}
}
} catch (JSONException e) {
e.printStackTrace();
}
i++;
lastNumber = i;
}
mAdapter.notifyDataSetChanged();
if(listener!=null)
listener.onDone(lastNumber);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (listener != null)
listener.onError(error.getMessage());
}
});
AppController.getInstance().addToRequestQueue(movieReq);
}
然后当您想要使用它时,请按以下方式调用它:
makeJsonObjectRequest(your_startList, your_endList, new JsonObjectListener() {
@Override
public void onDone(int lastNumber) {
/// Do every thing you want with lastNumber
}
@Override
public void onError(String error) {
hidePDialog();
}
});
}