Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code> public class uploadfile extends AsyncTask&lt;Void, Integer, String&gt; { private ProgressDialog dialog; protected Context mContext; long totalSize; public uploadfile(Context context) { mContext = context; } @Override protected void onPreExecute() { dialog = new ProgressDialog(mContext); dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL); dialog.setTitle("Chance"); dialog.setCancelable(false); //dialog.setIndeterminate(true); dialog.setMessage("uploading..."); dialog.show(); //dialog.setProgress(0); } @Override protected String doInBackground(Void... params) { HttpClient httpclient = new DefaultHttpClient(); HttpContext httpContext = new BasicHttpContext(); HttpPost httppost = new HttpPost("your url"); StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy( policy); String responseMessage = ""; try { CustomMultiPartEntity entity = new CustomMultiPartEntity(new ProgressListener() { @Override public void transferred(long num) { publishProgress((int) ((num / (float) totalSize) * 100)); } }); //post request to send the file FileBody file1 = new FileBody(new File("path of file")); entity.addPart("file", file1); totalSize = entity.getContentLength(); httppost.setEntity(entity); // DEBUG System.out.println( "executing request " + httppost.getRequestLine( ) ); HttpResponse response = httpclient.execute(httppost, httpContext); responseMessage = EntityUtils.toString(response.getEntity()); System.out.println( "Server response" + responseMessage ); } catch (SocketTimeoutException e) { e.printStackTrace(); responseMessage = "unreachable"; } catch (ConnectTimeoutException e) { e.printStackTrace(); responseMessage = "unreachable"; } catch (Exception e) { e.printStackTrace(); responseMessage = "unreachable"; } return responseMessage; } @Override protected void onProgressUpdate(Integer... progress) { dialog.setProgress((int) (progress[0])); } protected void onPostExecute(String result) { //result is the response back from "doInBackground" dialog.cancel(); //check if there were errors upoloading if (result.equalsIgnoreCase("unreachable")) { Toast.makeText(mContext, "Error while uploading...Please check your internet connection and retry", Toast.LENGTH_LONG).show(); return; } if(result.equals("[\"uploaded\"]")) { Toast.makeText(mContext, "uploaded", Toast.LENGTH_LONG).show(); finish(); } } } </code></pre> <p>CustomMultiPartEntity.java</p> <pre><code>public class CustomMultiPartEntity extends MultipartEntity { private final ProgressListener listener; public CustomMultiPartEntity(final ProgressListener listener) { super(); this.listener = listener; } public CustomMultiPartEntity(final HttpMultipartMode mode, final ProgressListener listener) { super(mode); this.listener = listener; } public CustomMultiPartEntity(HttpMultipartMode mode, final String boundary, final Charset charset, final ProgressListener listener) { super(mode, boundary, charset); this.listener = listener; } @Override public void writeTo(final OutputStream outstream) throws IOException { super.writeTo(new CountingOutputStream(outstream, this.listener)); } public static interface ProgressListener { void transferred(long num); } public static class CountingOutputStream extends FilterOutputStream { private final ProgressListener listener; private long transferred; public CountingOutputStream(final OutputStream out, final ProgressListener listener) { super(out); this.listener = listener; this.transferred = 0; } public void write(byte[] b, int off, int len) throws IOException { out.write(b, off, len); this.transferred += len; this.listener.transferred(this.transferred); } public void write(int b) throws IOException { out.write(b); this.transferred++; this.listener.transferred(this.transferred); } } } </code></pre>
    singulars
    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. This table or related slice is empty.
    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