how to Upload image into server in Android?

前端 未结 1 1887
梦毁少年i
梦毁少年i 2020-12-09 23:19

I am trying to Upload image Capture by Camera into server. server send response code = 200 but Image is not upload into server.

Code is :

private bo         


        
相关标签:
1条回答
  • 2020-12-09 23:59

    I using this:

    public class HttpClient extends AsyncTask<Void, Integer, Long> {
        private static final int PROGRESS_DIALOG = 0;
    
        public ProgressDialog dialog;
        public File file;
        protected Long doInBackground(Void... params) {
            for (File file : files) {
    
                foto = "/sdcard/CameraExample/" + file.getName();
                DefaultHttpClient httpclient = new DefaultHttpClient();
    
                HttpPost httppost = new HttpPost(urll);
    
    
    
                MultipartEntity mpEntity = new MultipartEntity(
                        HttpMultipartMode.BROWSER_COMPATIBLE);
    
                mpEntity.addPart("form_file", new FileBody(file, "image/jpeg"));
    
                httppost.setEntity(mpEntity);
    
                HttpResponse response;
                try {
    
                    response = httpclient.execute(httppost);
    
                    HttpEntity resEntity = response.getEntity();
    
                    if (resEntity != null) {
    
                    }
                    if (resEntity != null) {
                        resEntity.consumeContent();
                    }
                } catch (ClientProtocolException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
    
    
    
            }
            return null;
        }
    
        protected void onPostExecute(Long unused) {
            progressDialog.dismiss();
    
            ((Runnable) ctx ).run();
    
            super.onPostExecute(unused);
        }
    
        protected void onPreExecute() {
        progressDialog = new ProgressDialog(ctx);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progressDialog.setMessage("Загрузка фото...");
        progressDialog.setProgress(0);
        progressDialog.setMax(count);
        progressDialog.show();
        }
    
    }
    

    This code using that library:

    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.entity.mime.HttpMultipartMode;
    import org.apache.http.entity.mime.MultipartEntity;
    import org.apache.http.entity.mime.content.FileBody;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.util.EntityUtils;
    import org.apache.james.mime4j.message.Message;
    

    You can find this in Google. If you don't find - i can send you this libraries.

    0 讨论(0)
提交回复
热议问题