1

I need to send a list of byte[] as json parameter to .net(wcf) server. Is it possible? or should I do base64 encoding and proceed?

For example, I need to pass a sessionId(String) and datalist(ArrayList<byte[]>) in a json object. I am using Retrofit Library, creating request model POJO and sending. sessionId is recieved in the server, but for 'datalist', server is recieving null.

Thanks in Advance

DGN
  • 702
  • 3
  • 12

1 Answers1

0

Try this code, this is how to send JSON data to restful web service:

JSONArray jsonArray=new JSONArray();
ArrayList<byte[]>arrayList=new ArrayList<byte[]>();
Iterator<byte[]> itr=arrayList.iterator();
while (itr.hasNext()) {
        byte[] bs = (byte[]) itr.next();
        JSONObject temp=new JSONObject();
        temp.put("byte",bs.toString());
        jsonArray.put(temp);
}

// create HttpClient

HttpClient httpclient = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
StringEntity se = new StringEntity(jsonArray.toString());

se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
post.setEntity(se);
HttpResponse response = httpclient.execute(post);
InputStream inputStream = response.getEntity().getContent();
// convert inputstream to string
Arpit Agrawal
  • 321
  • 1
  • 2
  • 14
  • Arpit, this is not what I asked. I want to do something like this JSONObject jsonObject=new JSONObject(); jsonObject.put("sessionId", "some String"); jsonObject.put("datalist", "some list of byte[] here"); By Googling, I found I need to encode the bytes in order to send. i need a conformation on that whether that is necessary, because encoding makes the size larger – DGN Nov 18 '15 at 12:12
  • Sorry folk, I made a little mistake in typing the code. Check the edit. – Arpit Agrawal Nov 18 '15 at 12:14