Java 从onResponse返回一个值
如何将“complited1”或“strIsCompleted”的值作为布尔值返回,而不是“return true” “不要读这个”,谢谢:p 可用的段落有很多变体,但大多数都经过了某种形式的修改,通过注入,文字看起来甚至一点都不可信。如果你要使用一段文字,你需要确保在文本中间没有隐藏任何尴尬之处。Java 从onResponse返回一个值,java,android,android-volley,Java,Android,Android Volley,如何将“complited1”或“strIsCompleted”的值作为布尔值返回,而不是“return true” “不要读这个”,谢谢:p 可用的段落有很多变体,但大多数都经过了某种形式的修改,通过注入,文字看起来甚至一点都不可信。如果你要使用一段文字,你需要确保在文本中间没有隐藏任何尴尬之处。 public boolean GetStatusByCategoryID(final String categoryID) { final StringRequest req
public boolean GetStatusByCategoryID(final String categoryID) {
final StringRequest req = new StringRequest(Request.Method.GET, " http://164.132.6.62/api/Answer/CheckCompleteness?userid=9",
new Response.Listener < String > () {
@Override
public void onResponse(String response) {
try {
VolleyLog.v("Response:%n %s", response);
Toast.makeText(HomeView.this, response.toString(), Toast.LENGTH_SHORT).show();
JSONArray jsonMainArray = new JSONArray(response);
//check length of json array
if (jsonMainArray.length() > 0) {
for (int i = 0; i < jsonMainArray.length(); i++) {
JSONObject jsonObject = jsonMainArray.getJSONObject(i);
String strCategoryId = jsonObject.isNull("categoryId") ? "" : jsonObject.optString("categoryId");
String strIsCompleted = jsonObject.isNull("isCompleted") ? "" : jsonObject.optString("isCompleted").toString();
if (strCategoryId.equals(categoryID)) {
String complited1 = strIsCompleted;
System.out.println(complited1);
}
}
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
Toast.makeText(HomeView.this, "Server error", Toast.LENGTH_SHORT).show();
}
}) {
@Override
public Map < String, String > getHeaders() throws AuthFailureError {
HashMap < String, String > headers = new HashMap < String, String > ();
headers.put("Authorization", "bWVuZGltbXVzdGFmYTpMb3RyMyNMb2d5");
return headers;
}
};
//Add Network Request in request queue
RequestQueue mRequestQueue = Volley.newRequestQueue(getApplicationContext());
req.setRetryPolicy(new DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 20000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
mRequestQueue.add(req);
return true;
}
公共布尔GetStatusByCategoryID(最终字符串categoryID){
final StringRequest req=新StringRequest(Request.Method.GET)http://164.132.6.62/api/Answer/CheckCompleteness?userid=9",
新响应。侦听器(){
@凌驾
公共void onResponse(字符串响应){
试一试{
VolleyLog.v(“响应:%n%s”,响应);
Toast.makeText(HomeView.this,response.toString(),Toast.LENGTH_SHORT.show();
JSONArray jsonMainArray=新的JSONArray(响应);
//检查json数组的长度
if(jsonMainArray.length()>0){
for(int i=0;igetHeaders()抛出AuthFailureError{
HashMapheaders=newhashmap();
页眉.put(“授权”、“bWVuZGltbXVzdGFmYTpMb3RyMyNMb2d5”);
返回标题;
}
};
//在请求队列中添加网络请求
RequestQueue MRRequestQueue=Volley.newRequestQueue(getApplicationContext());
请求setRetryPolicy(新的DefaultRetryPolicy(
DefaultRetryPolicy.DEFAULT\u TIMEOUT\u MS*20000,
DefaultRetryPolicy.DEFAULT\u最大重试次数,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
mRequestQueue.add(请求);
返回true;
}
我不完全确定您的意思,但通过一种方法,我们可以通过更改其变量类型来更改它返回的内容
例如
方法2在被调用时会得到一个字符串,在打印时会说“hello”。正如我所理解的,您希望更改onResponse中的布尔值。所以你必须这样做
interface CustomCallback {
void onResponse(boolean parameter);
}
并将接口作为参数传递给方法,如下所示
public boolean GetStatusByCategoryID(final String categoryID, CustomCallback callback)
然后在你的回复或回复中打电话就可以了
callback.onResponse(/*true or false*/);
第一:当你说你想把一个字符串作为布尔值返回时,你是什么意思?您计划使用什么逻辑来决定哪些字符串对应于true,哪些字符串对应于false?其次,我不知道你的第二段是什么意思——最后需要编辑以确保易读性。这意味着我被提示插入更多的文本,因为我的问题与文本部分的大小相比包含太多的代码,很可能……如果字符串为真,则我需要获得“complited1”(是字符串),因为文本返回真(布尔值)如果complited1为false,则返回false,正如我告诉过你的。如果“compiled1”为true,则只需回调.onResponse(true);。因为您正在执行异步调用,不能直接返回值。
callback.onResponse(/*true or false*/);