私はサーバーにデータを投稿しようとしており、400のコードを取得しています。私はこれが、その悪い要求と、それが私が思う不正なjsonに起因することを意味します。 これは、私は、サーバーに問題があるか、私のコードが問題であるということならばだから今、私は知りませんJSON投稿しようとするとAndroidのURLConnectionが失敗する
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
を形成するように言われた方法です。私は前にアンドロイドHttpUrlConnectionに問題がありました。この上の任意の助けが理解されるであろう
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();
}
}
}
:
だからここに私のHTTPハンドラクラスです。
使用ボレーが正直なところ –
を要求ように、私はちょうど私が – MNM