[英]How to return string in java methods?
I have a problem where i am unable to return a string from this method.我有一个问题,我无法从这个方法返回一个字符串。 I was unsuccessful when I tried creating a new variable outside the Response.Listener.当我尝试在 Response.Listener 之外创建一个新变量时,我没有成功。 This is probably very simple but how do I go about returning a string from this method.这可能很简单,但我如何从这个方法返回一个字符串。 The string I want to return is the 'featured_img_url' string.我想返回的字符串是 'featured_img_url' 字符串。
public String secondServiceCall(String featuredmedia){
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
"http://www.gadgetsinnepal.com/wp-json/wp/v2/media/"+featuredmedia, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject nested_response) {
try {
JSONObject guilld = nested_response.getJSONObject("guid");
String featured_img_url = guilld.getString("rendered");
Toast.makeText(getApplicationContext(),"IMAGE :" + featured_img_url,Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
"ERROR "+error.getMessage(), Toast.LENGTH_LONG).show();
}
});
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjReq);
return featured_img_url;
}
update your code to:将您的代码更新为:
String featured_img_url = null;
public String secondServiceCall(String featuredmedia){
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.GET,
"http://www.gadgetsinnepal.com/wp-json/wp/v2/media/"+featuredmedia, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject nested_response) {
try {
JSONObject guilld = nested_response.getJSONObject("guid");
featured_img_url = guilld.getString("rendered");
Toast.makeText(getApplicationContext(),"IMAGE :" + featured_img_url,Toast.LENGTH_LONG).show();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(),
"ERROR "+error.getMessage(), Toast.LENGTH_LONG).show();
}
});
MySingleton.getInstance(getApplicationContext()).addToRequestQueue(jsonObjReq);
return featured_img_url;
}
Here, you should simply pass the instance that call this methods to execute the methods from the response.在这里,您应该简单地传递调用此方法的实例以执行响应中的方法。
So change the methods to :因此,将方法更改为:
public void secondServiceCall(String featuredmedia, final MyClass caller){
Note that this will return nothing.请注意,这不会返回任何内容。 And the caller
instance need to be final
to be used in the inner class JsonObjectRequest
.并且caller
实例需要是final
才能在内部类JsonObjectRequest
。
and in the response, you need to pass the value to the instance of MyClass
.在响应中,您需要将值传递给MyClass
的实例。 So add a method in MyClass
所以在MyClass
添加一个方法
public void setFeatureImgUrl(String featuredImgUrl){ ... }
and you just need to call this in the response.你只需要在响应中调用它。
public void onResponse(JSONObject nested_response) {
...
caller.setFeatureImgUrl(feature_img_url);
...
}
Note : This could be done with an Observer pattern but I know that some people doesn't like it.注意:这可以通过观察者模式来完成,但我知道有些人不喜欢它。 I could add an example of it if needed.如果需要,我可以添加一个示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.