I wrote this AsyncTask
class that sends an array of POST data to my php server with no problem. Now I want to extend it so that it also sends a file to same script (I have already the receive handling in the php file). What I mean is I want it to post DATA + FILE in one go. Something like multipart entity or something from HTML action to php script.
What would I need to add to this class so it can upload a file with other things?
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;
}
}