0

i need to send the json string {'post_id': 3} to my server. Im using a put method. when i run it i get a 500 error from my server. am i sending the json data correctly? and help please?

   HttpClient httpclient = new DefaultHttpClient();
   HttpPut httput = new HttpPut("my url");
   HttpParams params2 = httpclient.getParams();
   HttpConnectionParams.setConnectionTimeout(params2, 30000);
   String responseBody = "";
   HttpResponse response = null;
   try {
       String base64EncodedCredentials = "Basic " + Base64.encodeToString(
           ("username"+ ":" + "password").getBytes(),
           Base64.NO_WRAP);
       httput.setHeader("Authorization", base64EncodedCredentials);
       httput.setHeader(HTTP.CONTENT_TYPE, "application/json");
       JSONObject obja = new JSONObject();
       try {

            obja.put("post_id","3");

            httput.setEntity(new StringEntity(obj.toString(), "UTF-8"));
                   // Execute HTTP Post Request
                   response = httpclient.execute(httput);
            } catch (JSONException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } 

       ///

       HttpEntity entity= response.getEntity();
       Log.d("  ", " LIKE RESPONSE " + response.getStatusLine().getStatusCode());
askquestion
  • 171
  • 1
  • 14
Samantha
  • 199
  • 5
  • 15
  • 500 is an Internal Server Error. meaning something is going wrong server-side – panini Feb 28 '14 at 04:15
  • i also had the [same problem] (http://stackoverflow.com/questions/18840601/android-json-post-response-status-code-500-and-status-line-internal-server-er) and my back end guys were fixed that problem. looks noting wrong with you code.. check with the back end.. – Dinithe Pieris Feb 28 '14 at 04:33
  • may i know what is your protocol,http OR https? – Pankaj Arora Feb 28 '14 at 05:01

1 Answers1

1

you can try using Ion for make http request is more easy https://github.com/koush/ion , support headers and body params .also check the web service "500" is a Internal Server Error

ingyesid
  • 2,864
  • 2
  • 23
  • 21