title: how to send media file to wcf rest json service and get string return response in json format
body: <p><br/>
  <strong><em>Please don't mark it as duplicate as I am fighting for many days and have already tried lots of examples but not able to solve and getting confuse.I am new to WCF and android also</em></strong><br/>
  So I have created a WCF service with some get and post method like below</p>
  
  <pre><code>[OperationContract]
      [WebInvoke(Method = "POST",
         UriTemplate = "RegisterUser",
         BodyStyle = WebMessageBodyStyle.WrappedRequest,
         RequestFormat= WebMessageFormat.Json,
         ResponseFormat = WebMessageFormat.Json)]
      ResultSet RegisterUser(string EmailID, string Name,Stream profilepic, string Mobile, long IMEI);
  </code></pre>
  
  <p>I am calling this service method by android client as below </p>
  
  <p>MainActivity.java</p>
  
  <pre><code>public void doneOnClick(View v) throws FileNotFoundException,
          InterruptedException, JSONException {
      // Toast toast = new Toast(this);
      // gets IMEI of device ID
      tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
      imei = tm.getDeviceId();
  
      bMap = BitmapFactory.decodeFile(selectedImagePath);
      path = SaveImage.writeFileToInternalStorage(getApplicationContext(),
              bMap, "UserImage.png");
  
      name = nameV.getText().toString();
      mobile = mobileV.getText().toString();
      emailID = emailV.getText().toString();
  
      if (name.length() != 0 &amp;&amp; mobile.length() != 0 &amp;&amp; emailID.length() != 0
              &amp;&amp; path.length() != 0) {
          SharedPreferences shared = getSharedPreferences(PREFS, 0);
          Editor editor = shared.edit();
          editor.putString("UserPicPath", path);
          editor.putString("UserName", name);
          editor.putString("UserMobile1", mobile);
          editor.putString("UserEmail", emailID);
          editor.putString("IMEI", imei);
          editor.commit();
      }
  
      JSONArray jsonarr = new JSONArray();
  
      JSONObject jsonObj = new JSONObject();
      jsonObj.put("emailID", emailID);
      jsonObj.put("name", name);
      jsonObj.put("mobile", mobile);
      jsonObj.put("imei", imei);
      jsonarr.put(jsonObj);
      servicemethodname = "RegisterUser";
      DownloadWebPageTask bcktask = new DownloadWebPageTask();
      bcktask.execute(servicemethodname, jsonarr);
  }
  </code></pre>
  
  <p>and calling backgroundtask as</p>
  
  <pre><code>package com.example.wcfconsumer;
  
  import java.io.BufferedReader;
  import java.io.ByteArrayOutputStream;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.UnsupportedEncodingException;
  
  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.StringEntity;
  import org.apache.http.impl.client.DefaultHttpClient;
  import org.json.JSONArray;
  
  import android.graphics.Bitmap;
  import android.os.AsyncTask;
  import android.util.Base64;
  import android.util.Log;
  
  public class DownloadWebPageTask extends AsyncTask&lt;Object, Integer, String&gt; {
  
  private final static String SERVICE_URI = "http://192.168.0.100:80/Service1.svc/";
  
  protected void onPostExecute(String result) {
      MainActivity.emailV.setText(result);
  }
  
  @Override
  protected String doInBackground(Object... params) {
      JSONArray jsonparams = (JSONArray) params[1];
      String methodname = params[0].toString();
      InputStream is;
      try {
          DefaultHttpClient httpClient = new DefaultHttpClient();
          HttpPost httpPost = new HttpPost(SERVICE_URI + methodname);
          StringEntity se = new StringEntity(jsonparams.toString(), "UTF-8");
          se.setContentType("application/json;charset=UTF-8");
          httpPost.setEntity(se);
          Log.e("Gerhard", jsonparams.toString());
          HttpResponse httpResponse = httpClient.execute(httpPost);
          HttpEntity httpEntity = httpResponse.getEntity();
          is = httpEntity.getContent();
  
          InputStreamReader i = new InputStreamReader(is);
          BufferedReader str = new BufferedReader(i);
          String msg = str.readLine();
          Log.e("Gerhard", msg);
          return msg;
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
      }
      return null;
  }
  
  private String convertToString(Bitmap image) {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      image.compress(Bitmap.CompressFormat.JPEG, 100, bos);
      byte[] data = bos.toByteArray();
      String mediaString = Base64.encodeToString(data, Base64.URL_SAFE);
      return mediaString;
  }
  }
  </code></pre>
  
  <p>My question contains multiple part :<br/>
  1. How to send Image file along with other datatypes to RegisterUser method and get reponse in json format?<br/>
  2. Does same will work for video file as for the Image file?<br/>
  3. I want to return customdatatype from service(in this case ResultSet), is there something special I need to do for it?<br/></p>
  
  <p><em><strong>Please don't mark it as duplicate as I have already tried lots of examples but not able to solve and getting confuse.</em></strong><br/></p>
  
  <p>Please help me!!!    Many Many Many Thanks In Advance.<br/>
  Regards,<br/>
  Sourabh</p>