W
W
wolfak2016-01-31 15:23:11
Android
wolfak, 2016-01-31 15:23:11

How to send a POST file via HttpURLConnection?

I figured out in the previous request how to send a POST request with parameters to a PHP server using HttpURLConnection in Android. Now faced with the problem of sending a file (image or video file) with text parameters (user ID and login).
After the photo selection dialog, I have a URI and a Bitmap type. The choice of video has not yet been implemented.
I know that I need to change the request type to a multi type, but I can’t figure out how to change my code:

class SendLoginData extends AsyncTask<Void, Void, Void> {

        String resultString = null;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected Void doInBackground(Void... params) {
            try {
                String myURL = "http://site.ru/";
                String parammetrs = "param1=1&param2=XXX";
                byte[] data = null;
                InputStream is = null;

                try {
                    URL url = new URL(myURL);
                    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                    conn.setRequestMethod("POST");
                    conn.setDoOutput(true);
                    conn.setDoInput(true);

                    conn.setRequestProperty("Content-Length", "" + Integer.toString(parammetrs.getBytes().length));
                    OutputStream os = conn.getOutputStream();
                    data = parammetrs.getBytes("UTF-8");
                    os.write(data);
                    data = null;

                    conn.connect();
                    int responseCode= conn.getResponseCode();

                    ByteArrayOutputStream baos = new ByteArrayOutputStream();

                    if (responseCode == 200) {
                        is = conn.getInputStream();

                        byte[] buffer = new byte[8192]; // Такого вот размера буфер
                        // Далее, например, вот так читаем ответ
                        int bytesRead;
                        while ((bytesRead = is.read(buffer)) != -1) {
                            baos.write(buffer, 0, bytesRead);
                        }
                        data = baos.toByteArray();
                        resultString = new String(data, "UTF-8");
                    } else {
                    }



                } catch (MalformedURLException e) {

                    //resultString = "MalformedURLException:" + e.getMessage();
                } catch (IOException e) {

                    //resultString = "IOException:" + e.getMessage();
                } catch (Exception e) {

                    //resultString = "Exception:" + e.getMessage();
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            if(resultString != null) {
                Toast toast = Toast.makeText(getApplicationContext(), resultString, Toast.LENGTH_SHORT);
                toast.show();
            }

        }
    }

I hope for your help. I read a lot of information on the Internet, but did not understand.
Thank you.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
C
coden55, 2016-02-01
@wolfak

https://github.com/kevinsawicki/http-request - I recommend the library, uses HttpURLConnection, simple and convenient.
In your case it will be something like this:

HttpRequest request = HttpRequest.post("http://site.ru/");
request.part("param1", "1");
request.part("param2", "xxx");
request.part("file", new File("/test.txt"));
int status = request.code();
if(status == 200) {
System.out.println(request.body());
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question