title: send JSON to server via HTTP put request in android
body: <p>How to wrap given json to string and send it to server via Http put request in android?</p>
  
  <p>This is how my json look like. </p>
  
  <pre><code>    {
      "version": "1.0.0",
      "datastreams": [
          {
              "id": "example",
              "current_value": "333"
          },
          {
              "id": "key",
              "current_value": "value"
          },
          {
              "id": "datastream",
              "current_value": "1337"
          }
      ]
  }
  </code></pre>
  
  <p>above is my json array.</p>
  
  <p>below is how I wrote the code but, its not working</p>
  
  <pre><code>        protected String doInBackground(Void... params) {
              String text = null;
              try {
                  JSONObject child1 = new JSONObject();
                  try{
                      child1.put("id", "LED");
                      child1.put("current_value", "0");
  
  
                  } catch (JSONException e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
  
  
                  JSONArray jsonArray = new JSONArray();
  
                  jsonArray.put(child1);
  
                  JSONObject datastreams = new JSONObject();
                  datastreams.put("datastreams", jsonArray);  
  
                  JSONObject version = new JSONObject();
                  version.put("version", "1.0.0");
                  version.put("version", datastreams);
  
  
               HttpClient httpClient = new DefaultHttpClient();
               HttpContext localContext = new BasicHttpContext();
               HttpPut put = new HttpPut("url");
               put.addHeader("X-Apikey","");
               StringEntity se = new StringEntity( version.toString());  
               se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
  
               put.addHeader("Accept", "application/json");
               put.addHeader("Content-type", "application/json");
               put.setEntity(se);
  
  
               try{
  
                     HttpResponse response = httpClient.execute(put, localContext);
                     HttpEntity entity = response.getEntity();
                     text = getASCIIContentFromEntity(entity);
               }
                catch (Exception e) {
                   return e.getLocalizedMessage();
               }
  
  
          }catch (JSONException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
          } catch (UnsupportedEncodingException e1) {
              // TODO Auto-generated catch block
              e1.printStackTrace();
          }
              return text;
          }
  </code></pre>
  
  <p>please help on this </p>