Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
260 views
in Technique[技术] by (71.8m points)

android - Java HttpClient changing content-type?

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 + "
");
            }
            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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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));

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...