Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First of all, You need this <a href="http://www.java2s.com/Code/Jar/o/Downloadorgapachehttpcomponentshttpclient421jar.htm" rel="noreferrer">org.apache.httpcomponents.httpclient_4.2.1</a> jar file and import to your build path. </p> <p>You can do that in such a way. consider the following</p> <pre><code>import org.apache.http.NameValuePair; import org.apache.http.message.BasicNameValuePair; import org.json.JSONException; import org.json.JSONObject; </code></pre> <p>And the declaration :</p> <pre><code> private List&lt;NameValuePair&gt; nameValuePairs; private HttpURLConnection connection = null; private DataOutputStream outputStream = null; private String lineEnd = "\r\n"; private String twoHyphens = "--"; private String boundary = "*****"; int bytesRead, bytesAvailable, bufferSize; byte[] buffer; int maxBufferSize = 1 * 1024 * 1024; </code></pre> <p>And the source code is as follows :</p> <pre><code>try { FileInputStream fileInputStream = new FileInputStream(StickerToSend); URL url = new URL("Your URL"); connection = (HttpURLConnection) url.openConnection(); // Allow Inputs &amp; Outputs connection.setDoInput(true); connection.setDoOutput(true); connection.setUseCaches(false); // Enable POST method connection.setRequestMethod("POST"); connection.setRequestProperty("Connection", "Keep-Alive"); connection.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); outputStream = new DataOutputStream(connection.getOutputStream()); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ // Here you can add parameters as follows : outputStream.writeBytes(twoHyphens + boundary + lineEnd); // The keyword "type" is the key value and outputStream .writeBytes("Content-Disposition: form-data; name=\"type\"" + lineEnd); outputStream.writeBytes(lineEnd); // You can assign values as like follows : outputStream.writeBytes("Your value"); outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + lineEnd); ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream .writeBytes("Content-Disposition: form-data; name=\"sticker\";filename=\"" + StickerToSend + "\"" + lineEnd); outputStream.writeBytes(lineEnd); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); buffer = new byte[bufferSize]; // Read file bytesRead = fileInputStream.read(buffer, 0, bufferSize); while (bytesRead &gt; 0) { outputStream.write(buffer, 0, bufferSize); bytesAvailable = fileInputStream.available(); bufferSize = Math.min(bytesAvailable, maxBufferSize); bytesRead = fileInputStream.read(buffer, 0, bufferSize); } outputStream.writeBytes(lineEnd); outputStream.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); // int serverResponseCode = connection.getResponseCode(); String serverResponseMessage = connection.getResponseMessage(); Log.d("The server response message ", " Server Response" + serverResponseMessage); fileInputStream.close(); outputStream.flush(); // ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ InputStream is = connection.getInputStream(); // retrieve the response from server int ch; StringBuffer b = new StringBuffer(); while ((ch = is.read()) != -1) { b.append((char) ch); } String result = b.toString(); Log.i("Response", result); JSONObject jsonobject; try { jsonobject = new JSONObject(result); boolean isPosted = jsonobject.getJSONObject("response") .getString("httpCode").equals("200") ? true : false; if (isPosted) mHandler.sendEmptyMessage(POST_SUCCESS); else mHandler.sendEmptyMessage(POST_FAILURE); } catch (JSONException e) { Log.d("the xceptions ", "Xcep in posting status messages are : " + e.getMessage()); } outputStream.close(); } catch (Exception e) { Log.d("Xceptions", "Xceptions are upload video file " + e.getMessage()); } </code></pre> <p>This piece of code really works very well. I have checked this also. Please feel free if there is any issues.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload