我有一个问题,我无法从此方法返回一个字符串。当我尝试在Response.Listener之外创建一个新变量时,我没有成功。这可能非常简单,但我该如何从这个方法返回一个字符串。我想要返回的字符串是'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;
}
答案 0 :(得分:0)
将您的代码更新为:
[DllImport("user32.dll")]
public static extern int FindWindowEx(int hWnd1, int hWnd2, string lpsz1, string lpsz2);
[DllImport("user32.dll")]
public static extern int FindWindow(string lpClassName, string lpWindowName);
[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int Msg, int wParam, int lParam);
public class messagebox_control
{
const int WM_LBUTTONDOWN = 0x2201;
const int WM_LBUTTONUP = 0x0202;
const int BM_CLICK = 0x00F5;
public void Click_message()
{
int nhwnd = FindWindow("#32770", "웹 페이지 메시지");
if(nhwnd>0)
{
int hw1 = FindWindowEx(nhwnd, 0, "DirectUIHWND", "");
if(hw1>0)
{
int hw2 = FindWindowEx(hw1, 0, "CtrlNotifySink", "");
if(hw2>0)
{
while(true)
{
int hw3 = FindWindowEx(hw2, 0, "Button", "확인");
if (hw3>0)
{
SendMessage(hw3, BM_CLICK, 0, 1);
break;
}
hw2 = FindWindowEx(hw1, hw2, "CtrlNotifySink", "");
if (hw2==0)
{
break;
}
}
}
}
}
}
}
答案 1 :(得分:0)
在这里,您应该简单地传递调用此方法的实例来执行响应中的方法。
所以将方法更改为:
public void secondServiceCall(String featuredmedia, final MyClass caller){
请注意,这不会返回任何内容。并且caller
实例需要final
才能在内部类JsonObjectRequest
中使用。
并且在响应中,您需要将值传递给MyClass
的实例。所以在MyClass
public void setFeatureImgUrl(String featuredImgUrl){ ... }
你需要在回复中调用它。
public void onResponse(JSONObject nested_response) {
...
caller.setFeatureImgUrl(feature_img_url);
...
}
注意:这可以通过Observer模式完成,但我知道有些人不喜欢它。如果需要,我可以添加一个例子。