質問

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;
    }
}
役に立ちましたか?

解決

Ok as @greenapps suggested (credit goes to him) I solved like this.

Well not entirly solved, because I have to decode the file content at server side and save it manually on server.

so I jsut added the file content to the BasicNameValuePair that I already had:

String fileAsBase64 = Base64.encodeToString( convertToByteArray(mFile)
                , Base64.DEFAULT);

    mPostPairs.add(new BasicNameValuePair("filecontent", fileAsBase64));

And this is the method that converts it to byte array:

/**
 *  Reads a file and returns its content as byte array
 * @param file file that should be returned as byte array
 * @return byte[] array of bytes of the file
 */
public static byte[] convertTextFileToByteArray(File file) {
    FileInputStream fileInputStream = null;
    byte[] bFile = new byte[(int) file.length()];
    try {
        fileInputStream = new FileInputStream(file);
        fileInputStream.read(bFile);
        fileInputStream.close();
    }catch(Exception e){
        e.printStackTrace();
    } finally {
        try {
            fileInputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            fileInputStream = null;
        }
    }
    return bFile;
}

At the server side I do this:

$content = imap_base64 ($_POST["filecontent"]);

that takes care of decoding the content back to normal.

Hope this helps someone else too

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top