这是我的代码:
EC2API.java
public interface EC2API {
@Multipart
@POST("identify_k")
Call<JSONObject> identification_result(@Part MultipartBody.Part image,
@Part("k") RequestBody number);
}
EC2Client.java
public class EC2Client {
EC2API service;
RequestBody k;
MultipartBody.Part file;
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
public EC2Client(HashMap<String, Integer> dictionaryValues, File data)
throws ProtocolException, MalformedURLException {
service = generateRetrofit();
RequestBody fileRequest = RequestBody.create(MediaType.parse("multipart/form-data"), data);
file = MultipartBody.Part.createFormData("img", null, fileRequest);
k = RequestBody.create(MediaType.parse("multipart/form-data"), String.valueOf(dictionaryValues.get(IdentifyPlant.KEY_K)));
}
public void executeRequest(){
Call<JSONObject> webserviceResponse = service.identification_result(file, k);
webserviceResponse.enqueue(new Callback<JSONObject>() {
@Override
public void onResponse(Call<JSONObject> call, Response<JSONObject> response) {
Log.d("WEBSERVICE SUCCESSFUL!", response.raw().toString());
}
@Override
public void onFailure(Call<JSONObject> call, Throwable t) {
Log.d("WEBSERVICE ERROR!", "Error retrieving data from webservice");
t.printStackTrace();
}
});
}
private EC2API generateRetrofit(){
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://ec2-32-45-90-127.us-west-2.compute.amazonaws.com:8080/")
.addConverterFactory(GsonConverterFactory.create())
.client(client)
.build();
EC2API service = retrofit.create(EC2API.class);
return service;
}
}
当我执行请求时,这些是第一行:
POST http://ec2-32-45-90-127.us-west-2.compute.amazonaws.com:8080/identify_k http/1.1
Content-Type: multipart/form-data; boundary=beb8961f-4f2b-4fbd-9e95-9063d90df90f
...
如您所见,它正在将请求发送到http://ec2-32-45-90-127.us-west-2.compute.amazonaws.com:8080/identify_k
但稍后,在发送请求后,我会在日志中收到以下消息:
HTTP FAILED: java.net.ConnectException: Failed to connect to ec2-32-45-90-127.us-west-2.compute.amazonaws.com/32.45.90.127:8080
目的地地址以奇怪的方式被改变。可能是什么?
编辑:我已经使用Postman使用相同的细节尝试了它,并且请求看起来类似于Retrofit尝试发送的请求,但是我收到了成功的响应。所以它似乎是一个改造问题。以防万一:我已经更改了此问题的原始网址。