Java HttpClient changing content-type?

37,979

Solution 1

Change this:

StringEntity se = new StringEntity(json); 
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);

To this only:

httppost.setEntity(new StringEntity(json.toString(), HTTP.UTF_8));

Solution 2

easy :

import org.apache.http.client.methods.HttpPost;

httppost.setEntity(new StringEntity(json.toString(), org.apache.http.entity.ContentType.APPLICATION_JSON));
Share:
37,979
Bart
Author by

Bart

I am an IT enthousiast with an aviation/engineering background. Not sure how long I can still keep up saying I am not a developer. I caught myself sharing a Facebook promo on a t-shirt saying developers don't fix your computer. I do lot's of Python and Javascript. I love combining engineering and aviation with software development. Especially in rapid prototyping of apps.

Updated on June 04, 2020

Comments

  • Bart
    Bart almost 4 years

    I am building a small app for my Android phone to forward text messages to a webserver using a very basic REST interface.

    I am using the android 4.0.3 SDK. I developed the webservice in python using the Django and the Django restframework package. The setup is completely out of the box. There is basically one endpoint that receives a POST of a JSON object holding the message info (sender, body, date). I have tested the service using cURL with the following command:

    curl -X POST -H 'Content-Type: application/json' --data '{"sender":"+xxxxxxxx", "body": "test", "send_date":"2011-03-20 16:32:02"}' http://[...]/messages.json

    This all works fine and I get the expected response:

    {"body": "test", "send_date": "2011-03-20T16:32:02", "id": 25, "sender": "+xxxxxxxxxx"}

    Now I set up the android app. It is a simple BroadcastReceiver child class that includes a private AsyncTask class:

    private class httpPost extends AsyncTask<String, Void, JSONObject> {
        protected JSONObject doInBackground(String... args) {
            // Create a new HttpClient and Post Header
            HttpClient httpclient = new DefaultHttpClient();
            HttpParams myParams = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(myParams, 10000);
            HttpConnectionParams.setSoTimeout(myParams, 10000);
    
            String url = args[0];
            String json=args[1];            
            JSONObject JSONResponse = null;
            InputStream contentStream = null;
            String resultString = "";
    
            try {
                HttpPost httppost = new HttpPost(url);
                httppost.setHeader("Content-Type", "application/json");
                httppost.setHeader("Accept", "application/json");
    
                StringEntity se = new StringEntity(json); 
                se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
                httppost.setEntity(se);
    
                for (int i=0;i<httppost.getAllHeaders().length;i++) {
                    Log.v("set header", httppost.getAllHeaders()[i].getValue());
                }
    
                HttpResponse response = httpclient.execute(httppost);
    
                // Do some checks to make sure that the request was processed properly
                Header[] headers = response.getAllHeaders();
                HttpEntity entity = response.getEntity();
                contentStream = entity.getContent();
    
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
    
            //convert response to string
            try{
                BufferedReader reader = new BufferedReader(new InputStreamReader(contentStream,"iso-8859-1"),8);
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                contentStream.close();
                resultString = sb.toString();
            }catch(Exception e){
                e.printStackTrace();
            }
    
            Log.v("log", resultString);
            return new JSONObject();
        }
    
    
    }
    

    As you can see I am just starting to get familiar with Java and the android SDK so please bear with me. This setup actually worked fine in another app that I build to send JSON strings to a Neo4J webservice.

    The problem is that when I post the message via Android to my webservice, at some point the content-type gets changed from 'application/json' to 'application/json, application/json'. The log entry in the headers loop still outputs the correct values for each header, however, the webservice returns this error message:

    {"error": "Unsupported media type in request 'application/json, application/json'."}

    I am puzzled, any help is welcome.

  • Bart
    Bart about 12 years
    Thanks mate!! That was all I needed. @SJuan76 you were right as well, cheers guys, very helpful!
  • Danpe
    Danpe over 9 years
    Should be se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
  • Cjo
    Cjo almost 4 years
    Addition of HTTP.UTF_8 as charset fixed my issue ... thank you :)