AndroidアプリからWebServicesを使用してJavaアプリケーションに画像を送信しようとしています。サーバー側では AndroidでHttpPutが動作しない
私はこのコードを持っており、それがクライアントとしてポストマンを使用して正常に動作します:私は、サーバーへ画像をアップロードしようとしている私のアンドロイドアプリケーションを持っている一方@Path("/report")
public class ReportService {
@PUT
@Path("/put")
@Consumes(MediaType.APPLICATION_JSON)
public Report saveReport(String json){
return ReportDAO.saveReport(json);
}
:
try {
//Encode the image
String imagenPath = params[1];
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap myBitmap = BitmapFactory.decodeFile(imagenPath);
myBitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
//Create client,set url and entity
HttpClient httpClient = new DefaultHttpClient();
StringEntity entity = new StringEntity(encodedImage);
HttpPut httpPut = new HttpPut("http://10.0.2.2:8080/xxx/rest/report/put");
httpPut.setEntity(entity);
HttpResponse response = httpClient.execute(httpPut);
HttpEntity resp = response.getEntity();
}catch(Exception e){
e.printStackTrace();
}
}
Androidスタジオの仮想デバイスを使用していますが、GETメソッドで正常に動作していますが、POST/PUTで失敗しています。
私は例外もなく、メソッドはサーバーに到達しません。 EntityUtilsを使用して応答内の内容を確認すると、空のStringが取得されます。
郵便番号のURLをコピーして「http://10.0.2.2:8080」を「http://localhost:8080」に交換していますが、Androidアプリケーションからは届いていません。
ありがとうございました!
私はサーバーと仮想デバイスを同じマシン上で実行しています。実際のデバイスを使用する前にこの通信をテストしています。私はなぜ私がhttp://10.0.2.2:8080/xxx/rest/user/get/{name}を消費できるのか分からないが、PUT/POSTメソッドではない。ネットワークに問題がある場合、メソッドを取得しないでください。返信用のTy – Marcks1
ポート上の受信トラフィックと送信トラフィックをファイアウォールで確認し、存在しない場合はファイアウォールのルールを追加する必要があります。また、郵便配達員で確認してください。 –