Hi All,
This what I did to send values to JSON WCF SERVICE from android:
Wcf service method declaration:
[OperationContract]
[WebInvoke(Method="POST", RequestFormat = WebMessageFormat.Xml,
ResponseFormat = WebMessageFormat.Xml,
UriTemplate = "/TestMethod")]
bool TestMethod(CustomImage cust);
This function is giving the array of bytes of image to be uploading -
public JSONArray readBytes(InputStream inputStream) throws IOException, JSONException {
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
JSONArray array=new JSONArray();
int len = 0,i=0;
while ((len = inputStream.read(buffer)) != -1) {
ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
byteBuffer.write(buffer, 0, len);
byte[] b= byteBuffer.toByteArray();
array.put(i,Base64.encodeToString(b, Base64.DEFAULT));
i++;
}
return array;
}
Following is the way to pass class object to JSON WCF Service :
try
{
InputStream is = new BufferedInputStream (getResources().openRawResource(R.drawable.gtan));
JSONArray array=readBytes(is);
is.close();
URI uri = new URI(SERVICE_URI+ "/TestMethod");
JSONObject jo1 = new JSONObject();
jo1.put("fileName", "avi.jpg");
jo1.put("ImageBytes",array);
jo1.put("EmailID", "avinash1234@gmail.com");
jo1.put("SkinCareInterest", "Acne");
jo1.put("LightSource", "Sunny");
jo1.put("SkinType", "3");
jo1.put("DateOfBirth", "1987-09-15");
jo1.put("UserName", "BTBP");
jo1.put("Password", "BTBP");
HttpURLConnection conn = (HttpURLConnection) uri.toURL().openConnection();
conn.setConnectTimeout(5*1000*3600);
conn.setReadTimeout(5*1000*3600);
conn.setRequestProperty("Content-Type","application/json; charset=utf-8");
conn.setRequestProperty("Accept", "application/json");
conn.setRequestProperty("Connection", "Keep-Alive");
conn.setRequestProperty("User-Agent", "Pigeon");
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestMethod("POST");
conn.connect();
OutputStream os =conn.getOutputStream();
DataOutputStream out = new DataOutputStream(os);
out.write(jo1.toString().getBytes());
out.flush();
os.close();
int code = conn.getResponseCode();
String message = conn.getResponseMessage();
conn.disconnect();
}
catch(Exception ex)
{
ex.printStackTrace();
}
Thanks,
Avinash Patil