我编写了两个不同的代码来尝试将选定的文件上传到服务器,但是它们都没有上载列表中可用的所有文件,尽管这两个代码均显示上传完成而没有任何错误。请帮助修复它。
Thread up = new Thread(new Runnable() {
@Override
public void run() {
String content_type = getMimeType(filespath.get(ina));
OkHttpClient okHttpClient = new OkHttpClient();
RequestBody requestBody = RequestBody.create(MediaType.parse(content_type), new File(filespath.get(ina)));
RequestBody requestBody1 = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("type", content_type)
.addFormDataPart("myFile", filespath.get(ina).substring(filespath.get(ina).lastIndexOf("/") + 1), requestBody)
.addFormDataPart("location", titlevalue + "/")
.build();
Request request = new Request.Builder().url("MyPHP URL")
.post(requestBody1)
.build();
try {
Response response = okHttpClient.newCall(request).execute();
if (!response.isSuccessful()) {
Log.i(TAG, "Error");
}
} catch (Exception e) {
e.printStackTrace();
}
}
});
up.start();
filespath是字符串Arraylist,其中包含所有要上传文件的路径,并且该线程在相同的Forloop中调用。
尝试的其他方法如下:
HttpClient httpClient = new DefaultHttpClient();
MultipartEntityBuilder multipartEntity = MultipartEntityBuilder.create();
FileBody fileBody = new FileBody(new File(filespath.get(ina)));
multipartEntity.addPart("myFile", fileBody);
multipartEntity.addTextBody("location", titlevalue + "/");
HttpEntity httpEntity = multipartEntity.build();
HttpPost httpPost = new HttpPost("MyPHP URL");
httpPost.setEntity(httpEntity);
try {
HttpResponse httpResponse = httpClient.execute(httpPost);
Log.i(TAG, "Http Re" + httpResponse);
} catch (Exception e) {
e.printStackTrace();
}
我进行了几次试验来检查,就像我只选择了20张图片,全部上传并且我也得到了不错的答复一样。当我尝试处理40张图像时,我只能上传32张,且响应正常,而对于其余8张,则没有响应。 请提供指南,我想念的是什么。
查看上载的Logcat 9文件,在服务器上我仅发现4个。(编号为1,2,3,4)