post メソッドに問題があります。データがリクエストに追加されていないようです。私のjsonオブジェクト(以下のjObject)はあります{"FirstName":"John"}
が、応答は{"FirstName":"Peter"}
. このリクエストで POST が機能しないのはなぜですか?
@Override
protected Void doInBackground(Void... arg) {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost request = new HttpPost(someUrl);
ResponseHandler <String> responseHandler = new BasicResponseHandler();
request.setHeader("Content-type", "application/json");
StringEntity se = new StringEntity(jObject.toString());
se.setContentEncoding("UTF-8");
se.setContentType("application/json");
request.setEntity(se);
String authorizationString = "Basic " + Base64.encodeToString(("username" + ":" + "password").getBytes(), Base64.NO_WRAP);
request.addHeader("Authorization", authorizationString);
String response = httpClient.execute(request, responseHandler);
編集:最初にデータを取得する方法
// Initialize url and create connection
URL url = new URL(urlSpec);
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
byte[] auth = (email_login + ":" + password_login).getBytes();
String basic = Base64.encodeToString(auth, Base64.NO_WRAP);
httpConn.setRequestProperty("Authorization", "Basic " + basic);
// Retrieve data
try{
ByteArrayOutputStream out = new ByteArrayOutputStream();
InputStream in = connection.getInputStream();
if(connection.getResponseCode() != HttpURLConnection.HTTP_OK) {
return null;
}
int bytesRead = 0;
byte[] buffer = new byte[1024];
while ((bytesRead = in.read(buffer)) > 0) {
out.write(buffer, 0, bytesRead);
}
out.close();
return out.toByteArray();
} finally {
connection.disconnect();
}