title: Posting image and string data as multipart from android to server
body: <h2>I am trying to post data as <code>multipart</code> using <code>post method</code> from android client to server</h2>
  
  <hr>
  
  <ul>
  <li>Image part is working fine</li>
  <li>I am not able to get the data from edittext to be able to send as
  another key value</li>
  </ul>
  
  <hr>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>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&lt;String, Integer, String&gt; {
  
          @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();
  
          }
  
      }
  
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code>&lt;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" &gt;
  
      &lt;LinearLayout
          android:layout_width="match_parent"
          android:layout_height="168dp"
          android:layout_weight="0.41"
          android:orientation="vertical"
          tools:context=".MainActivity" &gt;
  
          &lt;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" /&gt;
  
          &lt;EditText
              android:id="@+id/editText1"
              android:layout_width="195dp"
              android:layout_height="wrap_content"
              android:paddingTop="50dp"
              android:layout_gravity="center"
              android:ems="10" /&gt;
  
      &lt;/LinearLayout&gt;
  
      &lt;Button
          android:id="@+id/SUBMIT_BUTTON_ID"
          android:layout_width="fill_parent"
          android:layout_height="wrap_content"
          android:layout_marginBottom="47dp"
          android:text="SUBMIT" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <hr>
  
  <p><strong>I am getting the error in the line</strong> ::</p>
  
  <pre><code>reqEntity.addPart("key1", NAME);
  </code></pre>
  
  <p><strong>as</strong></p>
  
  <pre><code>The method addPart(String, ContentBody) in the type MultipartEntity is not applicable for the arguments (String, String)
  </code></pre>
  
  <hr>
  
  <p><strong>how can i overcome this !</strong></p>