将数据传递给服务 - android?
Posted
技术标签:
【中文标题】将数据传递给服务 - android?【英文标题】:Pass data to service - android? 【发布时间】:2016-08-21 11:58:43 【问题描述】:我的 Activity 中有一个 AsyncTask,我调用 AsyncTask :
CallServicesSetQuestion task = new CallServicesSetQuestion();
task.execute(rb);
然后:
public class CallServicesSetQuestion extends AsyncTask<RequestPackage,String,String>
@Override
protected void onPreExecute()
super.onPreExecute();
@Override
protected String doInBackground(RequestPackage... params)
Intent response = new Intent(_Context, SendComment_Service.class);
response.putExtra("params", params[0]);<===== HERE
_Context.startService(response);
return content;
@Override
protected void onPostExecute(String result)
super.onPostExecute(result);
我需要将 RequestPackage 发送到服务,但我不能。我怎么不能发送它。
这是我的请求包:
public class RequestPackage
private String uri;
private String method = "GET";
private Map<String,String> params = new HashMap<>();
public String getUri()
return uri;
public void setUri(String uri)
this.uri = uri;
public String getMethod()
return method;
public void setMethod(String method)
this.method = method;
public Map<String, String> getParams()
return params;
public void setParams(Map<String, String> params)
this.params = params;
public void setParam(String key,String value)
params.put(key,value);
public String getEncodedParams()
StringBuilder sb = new StringBuilder();
for (String key:params.keySet())
String value = null;
try
value = URLEncoder.encode(params.get(key),"UTF-8");
catch (UnsupportedEncodingException e)
e.printStackTrace();
if (sb.length()>0)
sb.append("&");
sb.append(key + "=" + value);
return sb.toString();
【问题讨论】:
这里 response.putExtra("params", params[0]);我无法将 RequestPackage 发送到服务。 【参考方案1】:我觉得你需要把它序列化
public class RequestPackage implements Serializable ..
Intent response = new Intent(_Context, SendComment_Service.class);
Bundle bundle = new Bundle();
bundle.putSerializable("params", params[0]);
response.putExtras(bundle);
_Context.startService(response);
【讨论】:
【参考方案2】:Make Request Package Serializable or parseable Like this then you can send
public class RequestPackage implements Serializable
private String uri;
private String method = "GET";
private Map<String,String> params = new HashMap<>();
public String getUri()
return uri;
public void setUri(String uri)
this.uri = uri;
public String getMethod()
return method;
public void setMethod(String method)
this.method = method;
public Map<String, String> getParams()
return params;
public void setParams(Map<String, String> params)
this.params = params;
public void setParam(String key,String value)
params.put(key,value);
public String getEncodedParams()
StringBuilder sb = new StringBuilder();
for (String key:params.keySet())
String value = null;
try
value = URLEncoder.encode(params.get(key),"UTF-8");
catch (UnsupportedEncodingException e)
e.printStackTrace();
if (sb.length()>0)
sb.append("&");
sb.append(key + "=" + value);
return sb.toString();
【讨论】:
如何从服务中获取数据? 您可以使用Handler从服务中获取数据 非常感谢。在您的指导下,我使用此链接javacodegeeks.com/2014/01/…。以上是关于将数据传递给服务 - android?的主要内容,如果未能解决你的问题,请参考以下文章
将数据传递给另一个 Java 类 Android Studio