我编写了这个AsyncTask
类,它可以毫无问题地将一组 POST 数据发送到我的 php 服务器。现在我想扩展它,以便它也将文件发送到同一个脚本(我已经在 php 文件中进行了接收处理)。我的意思是我希望它一次性发布 DATA + FILE。像多部分实体或从 HTML 动作到 php 脚本的东西。
我需要在这个类中添加什么以便它可以上传包含其他内容的文件?
public class UpdateSnakeGameStatusTask extends AsyncTask<String, Integer, HttpResponse> {
private Context mContext;
private ArrayList<NameValuePair> mPairs;
/**
* @param context The context that uses this AsyncTask thread
* @param postPairs <b>NameValuePair</b> which contains name and post data
*/
public UpdateSnakeGameStatusTask(Context context, ArrayList<NameValuePair> postPairs) {
mContext = context;
mPairs = new ArrayList<NameValuePair>(postPairs);
}
@Override
protected HttpResponse doInBackground(String... params) {
HttpResponse response = null;
HttpPost httppost = new HttpPost(params[0]); //this is the URL
try {
httppost.setEntity(new UrlEncodedFormEntity(mPairs));
HttpClient client = new DefaultHttpClient();
response = client.execute(httppost);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
}