gpt4 book ai didi

java - 我有两个彼此内部的方法,如何从内部方法返回父级

转载 作者:行者123 更新时间:2023-12-01 09:34:59 25 4
gpt4 key购买 nike

我有这个 CustomRequest 类:

public class CustomRequest extends Request<JSONObject>{

private Listener<JSONObject> listener;
private Map<String, String> params;

public CustomRequest(String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}

public CustomRequest(int method, String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}

@Override
protected Map<String, String> getParams() throws com.android.volley.AuthFailureError {
return params;
};

@Override
protected void deliverResponse(JSONObject response) {
listener.onResponse(response);
}

@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
try {
String jsonString = new String(response.data,
HttpHeaderParser.parseCharset(response.headers));
return Response.success(new JSONObject(jsonString),
HttpHeaderParser.parseCacheHeaders(response));
} catch (UnsupportedEncodingException e) {
return Response.error(new ParseError(e));
} catch (JSONException je) {
return Response.error(new ParseError(je));
}
}

}

然后我有我的类 JSONRequest:

public class JSONRequest extends AppCompatActivity{
public String url = "http://192.168.1.101/apps/clients/service.php";
public Map<String, String> requestParameters;

public void setParameters(Map<String, String> Parameters) {
this.requestParameters = Parameters;
}

public JSONObject postRequest() {
CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.GET, this.url, this.requestParameters, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
return response;
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", error.getMessage());
}
});
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(20 * 1000, 1, 1.0f));

ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetwork = connectivityManager.getActiveNetworkInfo();
boolean isConnected = activeNetwork != null && activeNetwork.isConnectedOrConnecting();

if (isConnected) {
AppController.getInstance().addToRequestQueue(jsonObjectRequest);
} else {
Toast.makeText(getApplicationContext(), "You need internet connection to perform this", Toast.LENGTH_LONG).show();
}
}

}

我想要做的是在我的 postRequest 方法中,有一个名为 jsonObjectRequest 的 CustomRequest 类对象,我想将 JSONObject 响应变量从 onResponse 方法返回到父 postRequest 方法,我收到错误:

return response;

因为它告诉我该方法的返回类型是void,请告诉我我做错了什么以及正确的方法是什么。

我应该按以下方式使用我的类(class):

JSONRequest request = new JSONRequest();
request.setParameters(Parameters);
JSONObject result = request.postRequest();

提前致谢

最佳答案

it tells me that the method's return type is void,

是的,大多数回调都是这样定义的。通常不需要返回,而是根据结果采取行动。

I want to return the JSONObject response variable from the onResponse method to the parent postRequest method

同样,如果您重组调用,则无需返回任何内容。另外,该方法末尾没有 return 语句,因此您的代码开始时并不完全正确。

利用 Volley 来发挥你的优势,并传递它的 Listener 对象以“返回东西”。

旁注这不是/不应该是 Activity 类。如果您需要上下文,请创建一个构造函数。

public class JSONRequestService {

private final Context mContext; // TODO: Make constructor

final Response.ErrorListener errorListener = new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", error.getMessage());
};

// passthough a callback instead of returning
public void postRequest(Response.Listener<JSONObject> listener) {
CustomRequest jsonObjectRequest = new CustomRequest(Request.Method.GET, this.url, requestParameters, listener, errorListener);

// TODO: Add to request queue

}
}

然后,使用

JSONRequestService service = new JSONRequestService(getApplicationContext());
service.setParameters(Parameters);
service.postRequest(new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// response is now available where you want
} catch (JSONException e) {
e.printStackTrace();
}
});

现在,正如您所看到的,这种方法与直接发出 Volley 请求之间实际上没有太大区别。您所做的只是公开参数并将 URL、RequestQueue 和错误监听器隐藏在另一个类后面。

我什至可能建议对参数使用构建器模式方法,因为当然每个请求都使用不同的参数。

public JSONRequestService withParameters(Map<String, String> Parameters) {
this.requestParameters = Parameters;
return this;
}

现在,您可以通过执行来折叠 setter

JSONRequestService service = new JSONRequestService(getApplicationContext());

service.withParameters(Parameters).postRequest();

关于java - 我有两个彼此内部的方法,如何从内部方法返回父级,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39066388/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com