我一直在搜索,但我没有找到任何方法的实现,它允许发出 HTTP 发布请求,同时接收 URL 和数据作为函数的参数。
我的意思是,我找到的所有示例都是为一组特定的参数量身定制的,我需要的是在 AsyncTask 方法中获取一个 URL 和一个包含数据的数组,但是如何传递 url(字符串)参数和发布数据(数组)参数?
任何帮助或链接将不胜感激。
我一直在搜索,但我没有找到任何方法的实现,它允许发出 HTTP 发布请求,同时接收 URL 和数据作为函数的参数。
我的意思是,我找到的所有示例都是为一组特定的参数量身定制的,我需要的是在 AsyncTask 方法中获取一个 URL 和一个包含数据的数组,但是如何传递 url(字符串)参数和发布数据(数组)参数?
任何帮助或链接将不胜感激。
对于类似的情况,我使用以下模式:
import java.util.List;
import org.apache.http.NameValuePair;
import android.os.AsyncTask;
public class AsyncHttpPostTask extends AsyncTask<Void, Void, Boolean> {
private List<NameValuePair> httpPostParams;
private String postHref;
private String someOtherValue;
public AsyncHttpPostTask(final String postHref, final List<NameValuePair> httpPostParams, final String someOtherValue) {
super();
this.postHref = postHref;
this.httpPostParams = httpPostParams;
this.someOtherValue = someOtherValue;
}
@Override
protected Boolean doInBackground(final Void... params) {
// Use httpPostParams (or any other values you supplied to a constructor) in your actual Http post here
// ...
return true;
}
}
要使用 AsyncTask,请创建一个实例,为构造函数和调用 execute() 提供所需的参数:
new AsyncHttpPostTask("http://example.com/post", httpPostParams, otherValue).execute();