So I am trying to Post data to a server and I get a 400 code. I know this means that its a bad request and its due to a malformed json I think. So this is how I was told to form the json
curl -i -u admin:admin -H "Content-Type: application/json" -X POST –d '{"customer_id":1, "device_id":"1234", "notification_id":"NOTIFICATIONID123456", "operating_system_type":"IOS"}' localhost:8090/api/1.0/notificationInformation
So now I don't know if the server is the problem or that my code is the problem. I have had issues with the android HttpUrlConnection before.
So here is my http handler class:
public class MyHttpPost extends AsyncTask<String, Void, String> {
private String username;
private String password;
private String json = "{\"customer_id\": \"000234\" , \"device_id\":\"1234443\", \"notification_id\":\"fLqDFPgBHcs:APA91bHHIB7kyTRqR18pK78k81AbV211jdhIyNlWK-CmejFKIK6FZJgx4R-7uzyfYLTqi_jhqclks07nkkQFnORXOU28wJ5qC3GIIY_WPaNxKCxIUTltMaWihPGcvaeOgyW7669M3K1n\", \"operating_system_type\":\"Android\"}";
///api/1.0/notificationInformation
public MyHttpPost(){}
public MyHttpPost(String username, String password)
{
this.username = username;
this.password = password;
}
@Override
protected String doInBackground(String... params) {
String url = params[0];
postHttp(url);
return null;
}
public void postHttp(String myUrl)
{
Log.d("Button ", "Pushed");
try {
URL url = new URL(myUrl);
if (username!=null)
{
Authenticator.setDefault(new Authenticator() {
@Override
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password.toCharArray());
}
});
}
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// Allow Outputs (sending)
connection.setDoOutput(true);
Log.d("Allows input: ", Boolean.toString(connection.getDoInput()));
connection.setUseCaches(false);
// Enable POST method
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type", "application/json");
connection.setRequestProperty("charset", "utf-8");
DataOutputStream printout = new DataOutputStream(connection.getOutputStream());
//printout.write(json.toString().getBytes("UTF8"));
printout.write(json.getBytes("UTF8"));
printout.flush();
printout.close();
int statuscode = connection.getResponseCode();
Log.d("Response code: " , Integer.toString(statuscode));
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
Any help on this will be appreciated.