Question

I am trying to post JSON string to Linux server. However, it always says that no JSON object could be decoded. Back when I was testing at local host 10.0.2.2:5000. I can always get JSON object.

Is there anything I can do to make it work? Thank you.

The error is shown as below: ValueError: No JSON object could be decoded

Update: I think it has something to do with my android permission. In my manifest file, I've added

<uses-permission android:name="android.permission.INTERNET" />

Is there any other lines that I should've added to make the post request work?

To answer my own question:

Well, I've just resolved the problem. It turns out that I have to add one line: This works.

Here is my code:

                    conn = (HttpURLConnection) url.openConnection();
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
                    conn.setRequestMethod("POST");
                    conn.setUseCaches(false);
                    conn.connect();
Was it helpful?

Solution

Well, I've just resolved the problem. It turns out that I have to add one line:

conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");

This works.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top