将数据传递给服务 - android?

时间:2016-04-27 09:22:12

标签: android android-intent android-asynctask android-service

我的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才能提供服务,但我无法发送。如何发送它。

这是我的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();
    }
}

2 个答案:

答案 0 :(得分:0)

使请求包可序列化或可解析如下所示,您可以发送

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();
    }
}

答案 1 :(得分:0)

我认为你需要传递它序列化

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);