I created a JSON-Server. The server contains this:
{
"employees": [
{
"id": 1,
"name": "Pankaj",
"salary": "10000"
},
{
"name": "David",
"salary": "5000",
"id": 2
}
]
}
I've been trying to make a POST or a PUT request to that server without any luck.
My code is in a public class InfoLoader extends AsyncTaskLoader<String>
. My code works correctly if I do a GET request.
Code is shown here:
try{
url = new URL(this.url);
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
conn.setDoOutput(true);
conn.connect();
OutputStreamWriter out = null;
try{
out = new OutputStreamWriter(conn.getOutputStream());
} catch (IOException e) {
Log.e("InfoLoader", "can't get OutputStream", e);
}
String jsonInputString = "{\"name\": \"Lisa\",\"salary\": \"2000\"}";
out.write(jsonInputString);
out.close();
} catch (IOException e) {
Log.e("InfoLoader", "Fallo HTTP", e);
} finally {
conn.disconnect();
}
I'm able to connect and do a GET request without problem. So I know that I'm able to connect to the server. No, the server is NOT in a read only mode.
I'm using Android Studio, Java.
How do I make a POST or PUT request correctly? I'm not sure what should I do differently. I've search online but there isn't a lot of info about this problem. Also, there is this web that gives example of how to do it in a different way. Maybe it could help you help me.
I don't really think it's a Server problem. This JSON-Server shows your request, and i was able to make appear a "PUT" request, but it was ignored.
Thank you in advance for your help.