Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8018989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T21:15:39+00:00 2026-06-04T21:15:39+00:00

Below is my code for uploading a file to the server. But I’m getting

  • 0

Below is my code for uploading a file to the server. But I’m getting network exceptions even after several tries and even after adding strict mode.

I’m new to Android and don’t know how can I use the async task, which many people advised for such kind of network operation. Could anyone tell me that where I’m wrong in the code and where should I use async task?

package de.fileuploader;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
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.ByteArrayBody;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.protocol.BasicHttpContext;
import org.apache.http.protocol.HttpContext;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

@SuppressWarnings("unused")
 public class Android_helloActivity extends Activity {

private String newName = "SMSBackup.txt";
private String uploadFile = "/mnt/sdcard/SMSBackup.txt";
private String actionUrl = "http://192.168.1.8:8080/admin/admin/uploads";
// private String
// actionUrl="http://upload-file.shcloudapi.appspot.com/upload";
private TextView mText1;
private TextView mText2;
private Button mButton;

@Override
public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()  
        .detectDiskReads()  
        .detectDiskWrites()  
        .detectNetwork()   // or .detectAll() for all detectable problems  
        .penaltyLog()  
        .build());  
       StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()  
        .detectLeakedSqlLiteObjects()  
        .detectLeakedClosableObjects()  
        .penaltyLog()  
        .penaltyDeath()  
        .build()); 

        mText1 = (TextView) findViewById(R.id.myText2);
        mText1.setText("Upload\n" + uploadFile);
        mText2 = (TextView) findViewById(R.id.myText3);
        mText2.setText("To Server Location\n" + actionUrl);

        mButton = (Button) findViewById(R.id.myButton);
        mButton.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                        /* uploadFile(); */
                        try {
                                HttpClient httpClient = new DefaultHttpClient();
                                HttpContext localContext = new BasicHttpContext();
                                HttpPost httpPost = new HttpPost(actionUrl);

                                MultipartEntity entity = new MultipartEntity(
                                                HttpMultipartMode.BROWSER_COMPATIBLE);
                                entity.addPart("name", new StringBody(newName));
                                File file=new File(uploadFile);
                                entity.addPart("file", new FileBody(file));
                                //entity.addPart("file", new
                                ByteArrayBody(data,"myImage.jpg"));
                                entity.addPart("gps", new StringBody("35.6,108.6"));
                                httpPost.setEntity(entity);
                                HttpResponse response = httpClient.execute(httpPost,
                                                localContext);
                                BufferedReader reader = new BufferedReader(
                                                new  
                               InputStreamReader(response.getEntity().getContent(), 
                                 "UTF-8"));

                                String sResponse = reader.readLine();
                                Log.i("info", "test");
                                  } catch (Exception e) {
                                // Log.e("exception", e.printStackTrace());
                                 e.printStackTrace();
                                showDialog("" + e);
                               } 
                               }
                               });
                               }


                          private void showDialog(String mess) {
                new AlertDialog.Builder(Android_helloActivity.this).setTitle("Message")
                         .setMessage(mess)
                        .setNegativeButton("Exit", new  DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which)      {
                                }
                        }).show();
 }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T21:15:40+00:00Added an answer on June 4, 2026 at 9:15 pm
    mButton.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {
    
    new UploadImageTask().execute(); // initialize asynchronous task
    
    }});
    
    //Now implement Asynchronous Task
    
    
    public class Get_User_Data extends AsyncTask<Void, Void, Void> {
    
                private final ProgressDialog dialog = new ProgressDialog(
                MyActivity.this);
    
        protected void onPreExecute() {
            this.dialog.setMessage("Loading...");
            this.dialog.setCancelable(false);
            this.dialog.show();
        }
            @Override
            protected Void doInBackground(Void... params) {
    
                        uploadImage(); // inside the method paste your file uploading code
                return null;
            }
    
            protected void onPostExecute(Void result) {
    
                // Here if you wish to do future process for ex. move to another activity do here
    
                if (dialog.isShowing()) {
                    dialog.dismiss();
                }
    
            }
        }
    

    For more information refer this link http://vikaskanani.wordpress.com/2011/01/29/android-image-upload-activity/

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I was uploading my file using below code,it works fine but some time it
Iam using below code for uploading images on the remote server.When I use below
I have a JavaScript function below where it displays a message after file uploading
Im using the below code for uploading -(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary*)info { NSData * dt =
Below code is the response from the server: xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance xmlns:xsd=http://www.w3.org/2001/XMLSchema><soap:Body><SendCardDetailsResponse xmlns=http://tempuri.org/><SendCardDetailsResult><ROOT xmlns=><DocumentElement><res-auth><auth-data count='1' ><**attribute
I tried uploading a picture to a directory in my server with the code
Can I use the PHP code below if the PDF file is not on
In my code attached below, I'm trying to upload a file via ASP.NET. I
I am trying to create a ftp client with uploading a file capability. But
I am using apache's FTP client to upload file to the ftp server. But

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.