0

I am trying to post a single image to my local server from android


  • I had tried this here few days ago and with my previous Stackoverflow post here
  • I was able to get the work done
  • Today when i retry this i am getting errors again

MainActivity.java

public class MainActivity extends Activity {

    Button submit;
    ProgressDialog pDialog;
    InputStream is;

    ImageView imageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        submit = (Button) findViewById(R.id.SUBMIT_BUTTON_ID);

        imageView = (ImageView) findViewById(R.id.imageView1);

        submit.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                new MainTest().execute();


            }
        });
    }



    /**
     * Method to post the image to the server.
     * U will have to change the url which will accept the image data.
     * @throws IOException 
     */
    public void postImageData() {


        try
        {

        Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher); 

        HttpClient httpClient = new DefaultHttpClient();
        HttpPost postRequest = new HttpPost("http://10.0.2.2:7002/Details/");
        MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
        try{
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            bitmapOrg.compress(CompressFormat.JPEG, 75, bos);
            byte[] data = bos.toByteArray();
            ByteArrayBody bab = new ByteArrayBody(data, "forest.jpg");
            reqEntity.addPart("key", bab);
        }
        catch(Exception e){
            //Log.v("Exception in Image", ""+e);
            reqEntity.addPart("picture", new StringBody(""));
        }
        postRequest.setEntity(reqEntity);       
        HttpResponse response = httpClient.execute(postRequest);
        BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
        String sResponse;
        StringBuilder s = new StringBuilder();
        while ((sResponse = reader.readLine()) != null) {
            s = s.append(sResponse);
        }
    }catch(Exception e){
        e.getStackTrace();
    }

















    }
    public class MainTest extends AsyncTask<String, Integer, String> {

        @Override
        protected void onPreExecute() {
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading..");
            pDialog.setIndeterminate(true);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {

            postImageData();

            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            // TODO Auto-generated method stub

            super.onPostExecute(result);
            // data=jobj.toString();
            pDialog.dismiss();

        }

    }

}

Log::

12-12 13:55:30.430: E/dalvikvm(541): Could not find class 'org.apache.http.entity.mime.MultipartEntity', referenced from method com.example.datapostingproject.MainActivity.postImageData
12-12 13:55:30.430: W/dalvikvm(541): VFY: unable to resolve new-instance 747 (Lorg/apache/http/entity/mime/MultipartEntity;) in Lcom/example/datapostingproject/MainActivity;
12-12 13:55:30.440: D/dalvikvm(541): VFY: replacing opcode 0x22 at 0x0016
12-12 13:55:30.440: D/dalvikvm(541): Making a copy of Lcom/example/datapostingproject/MainActivity;.postImageData code (297 bytes)
12-12 13:55:30.650: D/dalvikvm(541): GC freed 565 objects / 50640 bytes in 87ms
12-12 13:55:30.871: D/dalvikvm(541): GC freed 45 objects / 1728 bytes in 91ms
12-12 13:55:55.350: W/dalvikvm(541): threadid=15: thread exiting with uncaught exception (group=0x4001b188)
12-12 13:55:55.350: E/AndroidRuntime(541): Uncaught handler: thread AsyncTask #1 exiting due to uncaught exception
12-12 13:55:55.850: E/AndroidRuntime(541): java.lang.RuntimeException: An error occured while executing doInBackground()
12-12 13:55:55.850: E/AndroidRuntime(541):  at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-12 13:55:55.850: E/AndroidRuntime(541):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
12-12 13:55:55.850: E/AndroidRuntime(541):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
12-12 13:55:55.850: E/AndroidRuntime(541):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
12-12 13:55:55.850: E/AndroidRuntime(541):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
12-12 13:55:55.850: E/AndroidRuntime(541):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
12-12 13:55:55.850: E/AndroidRuntime(541):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
12-12 13:55:55.850: E/AndroidRuntime(541):  at java.lang.Thread.run(Thread.java:1096)
12-12 13:55:55.850: E/AndroidRuntime(541): Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.mime.MultipartEntity
12-12 13:55:55.850: E/AndroidRuntime(541):  at com.example.datapostingproject.MainActivity.postImageData(MainActivity.java:99)
12-12 13:55:55.850: E/AndroidRuntime(541):  at com.example.datapostingproject.MainActivity$MainTest.doInBackground(MainActivity.java:154)
12-12 13:55:55.850: E/AndroidRuntime(541):  at com.example.datapostingproject.MainActivity$MainTest.doInBackground(MainActivity.java:1)
12-12 13:55:55.850: E/AndroidRuntime(541):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-12 13:55:55.850: E/AndroidRuntime(541):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
12-12 13:55:55.850: E/AndroidRuntime(541):  ... 4 more
12-12 13:55:56.010: I/dalvikvm(541): threadid=7: reacting to signal 3
12-12 13:55:56.010: E/dalvikvm(541): Unable to open stack trace file '/data/anr/traces.txt': Permission denied

Note ::

Also i have used the Jar file from here

httpmime-4.2.3.jar ..... link

Seems i have followed the steps correct..... but what wrong am i doing here

Community
  • 1
  • 1
smriti3
  • 871
  • 2
  • 15
  • 35

2 Answers2

1

Clearly Caused by: java.lang.NoClassDefFoundError: org.apache.http.entity.mime.MultipartEntity

this class is not available to you in runtime, add Mime jar to lib forlder and add to your build path, this would reolve this issue.

Techfist
  • 4,314
  • 6
  • 22
  • 32
  • True .......... Actually i had added the Jar file from.... right click ---Add jar --- .... but it had not solved my problem then copying the jar file manually to libs folder solved the problem ... thanks – smriti3 Dec 12 '13 at 08:52
1

First of all remove that jar file:

And java.lang.NoClassDefFoundError: means you have not properly add jar file to your project. So

After that import that jar file:

Right Click -> Properties -> Java Build path -> Add External Jar-> Apply ->Ok

And also add that jar file to your libs folder.

Piyush
  • 18,895
  • 5
  • 32
  • 63
  • Actually i had added the Jar file from.... right click ---Add jar --- .... but it had not solved my problem then copying the jar file manually to libs folder solved the problem – smriti3 Dec 12 '13 at 08:52