[英]Return a string from an anonymous class
I have an android project in which I use a class to send http requests using the Volley library. 我有一个android项目,其中使用类通过Volley库发送http请求。 It is called
WebServicesAdapter
. 它称为
WebServicesAdapter
。 I used a callback in it to return a value to the called activity but it prevents the new activity from starting because of some problems in the context. 我在其中使用了回调以将值返回给被调用的活动,但是由于上下文中的某些问题,它阻止了新活动的启动。 How can I return a string without using a callback?
如何在不使用回调的情况下返回字符串?
Below is my code. 下面是我的代码。
successcallback
is the callback when I want to return a string instead. successcallback
是当我想返回一个字符串时的回调。
public class WebServiceAdapter {
private static String BASE_URI = "http://192.168.42.94/getvoize/index.php";
private RequestQueue rQueue;
public String responseString;
public String status;
Context context;
public WebServiceAdapter(Context context){
this.context = context;
status = "new";
rQueue = Volley.newRequestQueue(context);
}
private WebServiceInterface wsi;
public void sendGetRequest(String page,Map<String,String> map, WebServiceInterface i){
wsi = i;
String query = "";
if(!map.isEmpty()){
for (Map.Entry<String, String> entry : map.entrySet())
{
query =query + entry.getKey()+"="+entry.getValue()+'&';
}
}
if(query.length() != 0)
query = query.substring(0,query.length()-1);
StringRequest sRequest = new StringRequest(Request.Method.GET,BASE_URI+page+"?"+query,
new Response.Listener<String>() {
@Override
public void onResponse(String response){
wsi.successCallback(response,context);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error){
wsi.errorCallback("failed",context);
}
});
rQueue.add(sRequest);
}
private Map<String,String> parameter;
private Map<String,String> headers;
public void sendPostRequest(String page,Map<String,String> body,Map<String,String> header,WebServiceInterface i){
wsi = i;
parameter = body;
headers = header;
Log.d("place", "Inpost");
StringRequest myReq = new StringRequest(Request.Method.POST,
BASE_URI+page,
new Response.Listener<String>() {
@Override
public void onResponse(String response){
wsi.successCallback(response, context);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error){
wsi.errorCallback("Failed", context);
}
}
) {
@Override
protected Map<String,String> getParams(){
Map<String,String> params = parameter;
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = headers;
return params;
}
};
rQueue.add(myReq);
}
You have not posted activity code where you are trying to start new activity. 您尚未在尝试开始新活动的地方张贴活动代码。 It would be helpful if you can share that piece here along with error logs if any.
如果您可以在此处与错误日志(如果有的话)共享该部分,将很有帮助。
In case you are trying to launch an activity from callback in the calling Activity, remember to do it in Main Thread (assuming the web servie is called from thread other than main thread). 如果您试图从调用的Activity中的回调启动一个Activity,请记住在Main Thread中进行此操作(假定从非主线程调用了Web服务)。
Other input worth mentioning is - from your callback to activity, context is not required to be passed back. 值得一提的其他输入是-从回调到活动,不需要将上下文传递回去。 It would be readily available there in activity.
它可以随时在活动中使用。
you can use below code: 您可以使用以下代码:
runOnUiThread(new Runnable() {
public void run() {
//startActivity code here.
}
});
Define your callback interface (I assume you already have this and WebServiceInterface
is the callback interface). 定义您的回调接口(我假设您已经有这个接口,并且
WebServiceInterface
是回调接口)。
Your Activity
should implement this interface. 您的
Activity
应实现此接口。
Add a parameter WebServiceInterface
to the WebServiceAdapter
constructor. 将参数
WebServiceInterface
添加到WebServiceAdapter
构造函数。 The constructor should store this in a private member variable. 构造函数应将此存储在私有成员变量中。
When your Activity
creates an instance of WebServiceAdapter
, it should pass this
as the WebServiceInterface
parameter. 当您的
Activity
创建WebServiceAdapter
的实例时,应将this
作为WebServiceInterface
参数传递。
WHen you want to call back the Activity
, just make your calls on the stored private member variable. 如果您想回调
Activity
,只需对存储的私有成员变量进行调用即可。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.