1

I am trying to post data as multipart using post method from android client to server


  • Image part is working fine
  • I am not able to get the data from edittext to be able to send as another key value

MainActivity.java

public class MainActivity extends Activity {

    Button submit;
    ProgressDialog pDialog;
    InputStream is;

    EditText name;
    ImageView imageView;

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

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

        name = (EditText) findViewById(R.id.editText1);
        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.image); 
            String NAME = name.getText().toString();

            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, "image.jpg");
                reqEntity.addPart("key", bab);
                reqEntity.addPart("key1", NAME);
            }
            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();

        }

    }

}

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="168dp"
        android:layout_weight="0.41"
        android:orientation="vertical"
        tools:context=".MainActivity" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="match_parent"
            android:layout_height="101dp"
            android:layout_marginTop="32dp"
            android:clickable="false"
            android:src="@drawable/image" />

        <EditText
            android:id="@+id/editText1"
            android:layout_width="195dp"
            android:layout_height="wrap_content"
            android:paddingTop="50dp"
            android:layout_gravity="center"
            android:ems="10" />

    </LinearLayout>

    <Button
        android:id="@+id/SUBMIT_BUTTON_ID"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="47dp"
        android:text="SUBMIT" />

</LinearLayout>

I am getting the error in the line ::

reqEntity.addPart("key1", NAME);

as

The method addPart(String, ContentBody) in the type MultipartEntity is not applicable for the arguments (String, String)

how can i overcome this !

smriti3
  • 871
  • 2
  • 15
  • 35
  • where is `reqEntity` declared and what is `NAME`? – Raghunandan Dec 13 '13 at 06:13
  • @Raghunandan ..... NAME is the variable name .... i am trying to get the values of edittext and pass it here ( i am a noob) .... i am not able to determine how to send another key value along with image .... can u guide me please – smriti3 Dec 13 '13 at 06:18
  • @Prince ....... I have updated the answer ..... thats all the code i have – smriti3 Dec 13 '13 at 06:19
  • @smriti3 why always .... unwanted. – Raghunandan Dec 13 '13 at 06:19
  • 1
    @smriti3 this is working code for me : http://stackoverflow.com/questions/12422541/how-to-send-multiple-images-to-server-using-multipartentity-from-android – Hardik Joshi Dec 13 '13 at 06:29

2 Answers2

3

You have

String NAME = name.getText().toString();
reqEntity.addPart("key1", NAME);

public void addPart(String name,ContentBody contentBody)

You have wrong params for addPart. The second param should be ContentBody

So change to

 reqEntity.addPart("key1", new StringBody(NAME));

Reference

http://hc.apache.org/httpcomponents-client-ga/httpmime/apidocs/org/apache/http/entity/mime/content/ContentBody.html

Check the below if it helps

http://www.androidhive.info/2011/10/android-making-http-requests/

Raghunandan
  • 132,755
  • 26
  • 225
  • 256
  • @smriti3 did you check how you receive on the server side. As was in your previous case that might be a problem. Check the second link in my post. – Raghunandan Dec 13 '13 at 06:53
  • @smriti3 do check the docs for MultiPartEntity and there is no need for ..... in your comments. Put a single `.`. – Raghunandan Dec 13 '13 at 06:55
  • @smriti3 i see nothing wrong on the android side other than the suggested edit. – Raghunandan Dec 13 '13 at 09:54
  • @ Raghunandan ...... i have come up with a different approach to solve this .... please have a look at this link ...... http://stackoverflow.com/q/20565971/2901850 – smriti3 Dec 13 '13 at 11:59
  • @smriti3 surely problem on server side. You need to handle the request at the server end. I am pretty sure android code is alright – Raghunandan Dec 13 '13 at 12:01
  • 1
    @ Raghunandan ......... after spending one day on this .... i resolved this ..... problem was on the server side ... as u guided me :) – smriti3 Dec 13 '13 at 12:37
  • 1
    @smriti3 - I am glad that StringBody solved your problem. Keep coding and help the community! – Deminem Dec 13 '13 at 15:19
1

The above code snippet gives error because if you want to send text in the multi-part then wrap inside the StringBody class.

Do this

reqEntity.addPart("key1", new StringBody("Your String goes here..."));

instead of

reqEntity.addPart("key1", NAME);
Deminem
  • 672
  • 8
  • 19