title: Posting multipart form data via Android AsyncTask
body: <p>My issue is with the writeArgsToConn() function.....i think. I cant figure out how to implement it. </p>
  
  <p>I am trying to post multipart-formdata from an Android device using AsyncTask class. Can anyone help with this? I want to stay away from the depreciated org.apache.http.legacy stuff and stick with up-to-date Android libraries.</p>
  
  <p>I was able to use similar implementation for a class called DoPostJSON which used Content-Type: application/json and that class works fine.</p>
  
  <p>Same question but on Reddit: <a href="https://redd.it/49qqyq" rel="nofollow">https://redd.it/49qqyq</a></p>
  
  <p>I had issues with getting nodejs express server to detect the parameters being sent in. My DoPostJSON class worked fine and my nodejs server was able to detect parameters...for some reason DoPostMultiPart doesnt work and nodejs server cant see paramters being passed in. I feel like I am using the library the wrong way.</p>
  
  <pre><code>public class DoPostMultiPart extends AsyncTask&lt;JSONObject, Void, JSONObject&gt; implements Post{
  
      @Override
      public HttpURLConnection createConn(String action) throws Exception{
          URL url = new URL(Utils.host_api + action);
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod("POST");
  
          conn.setDoInput(true);
          conn.setDoOutput(true);
  
          conn.setRequestProperty("Connection", "Keep-Alive");
          conn.setRequestProperty("Cache-Control", "no-cache");
  
          conn.setReadTimeout(35000);
          conn.setConnectTimeout(35000);
          return conn;
      }
  
      @Override
      public JSONObject getResponse(HttpURLConnection conn) throws Exception {
          int responseCode = conn.getResponseCode();
          String response = "";
          if (responseCode == HttpsURLConnection.HTTP_OK) {
              InputStream in = new BufferedInputStream(conn.getInputStream());
              BufferedReader responseStreamReader = new BufferedReader(new InputStreamReader(in));
              String line = "";
              StringBuilder stringBuilder = new StringBuilder();
              while ((line = responseStreamReader.readLine()) != null)
                  stringBuilder.append(line).append("\n");
              responseStreamReader.close();
              response = stringBuilder.toString();
          } else {
              throw new Exception("response code: " + responseCode);
          }
          conn.disconnect();
          return new JSONObject(response);
      }
  
      // TODO: fix this function
      @Override
      public void writeArgsToConn(JSONObject args, HttpURLConnection conn) throws Exception {
          // define paramaters
          String fullname = args.getString("fullname");
          String email = args.getString("email");
          String password = args.getString("password");
          String confpassword = args.getString("confpassword");
          Bitmap pic = (Bitmap) args.get("pic");
  
          // plugin paramters into request
          OutputStream os = conn.getOutputStream();
          // how do I plugin the String paramters???
          pic.compress(Bitmap.CompressFormat.JPEG, 100, os); // is this right???
          os.flush();
          os.close();
      }
  
      @Override
      protected JSONObject doInBackground(JSONObject... params) {
          JSONObject args = params[0];
          try {
              String action = args.getString("action");
              HttpURLConnection conn = createConn(action);
              writeArgsToConn(args, conn);
              return getResponse(conn);
          } catch (Exception e) {
              Utils.logStackTrace(e);
              return null;
          }
      }
  }
  </code></pre>