我正在尝试使用 POST 命令将图片上传到我的Server。为此,我使用OkHttp Recipes中的代码。
private static final MediaType MEDIA_TYPE_JPG=MediaType.parse("image/jpg");
private final OkHttpClient client = new OkHttpClient();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_equipment);
PATH_TO_EXTERNAL = Environment.getExternalStorageDirectory().getPath();
FULL_PATH = PATH_TO_EXTERNAL + IMAGE_PATH;
file = new File(FULL_PATH);
body = RequestBody.create(MEDIA_TYPE_JPG, file);
run(); /* Calling it on Background Thread*/
}
public void run() throws Exception {
RequestBody requestBody = new MultipartBuilder().type(MultipartBuilder.FORM)
.addFormDataPart("aadcs", "acsd.jpg",body)
.build();
Request request = new Request.Builder().url(UPLOAD_URL).post(requestBody).build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful()) { throw new IOException("Unexpected code " + response); }
Log.d("Response", response.body().string());
}
但问题是我无法得到任何响应,即当我逐行调试代码时,它永远不会超出这一行:
Response response = client.newCall(request).execute();
。
当我从POSTMAN等任何在线REST客户端测试时,Url工作正常。 我无法弄清楚我在这里做错了什么。
HELP !!!