我在我的应用程序中添加了一个屏幕,其中用户从图库或相机上传图像并提供一些细节并将其发送到服务器,但问题是当我每次点击提交按钮时出现问题" SocketTimeOutException&# 34;但邮件发送到服务器。请帮忙。
我之前能够上传表格但现在没有。我无法解决的问题是什么。
代码:
private void execMultipartPost() throws Exception {
RequestBody requestBody;
if (filePath != null) {
File file = new File(filePath);
String contentType = file.toURL().openConnection().getContentType();
fileBody = RequestBody.create(MediaType.parse(contentType), file);
filename = "file_" + System.currentTimeMillis() / 1000L;
Log.d("TAG", "execMultipartPost: " + filePath);
requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("destination_type", selectedDescription)
.addFormDataPart("admin_id", admin_id)
.addFormDataPart("date", txtDate)
.addFormDataPart("time", txtTime)
.addFormDataPart("description", txtDescription)
.addFormDataPart("image", filename + ".jpg", fileBody)
.addFormDataPart("user_id", user_id)
.addFormDataPart("api_token", api_token)
.build();
} else {
requestBody = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("destination_type", selectedDescription)
.addFormDataPart("admin_id", admin_id)
.addFormDataPart("date", selectdate.getText().toString())
.addFormDataPart("time", selecttimehr.getText().toString())
.addFormDataPart("description", description.getText().toString())
.addFormDataPart("image", "null")
.addFormDataPart("user_id", user_id)
.addFormDataPart("api_token", api_token)
.build();
}
okhttp3.Request request = new okhttp3.Request.Builder()
.url(Constansts.LETS_MELDEN)
.post(requestBody)
.build();
OkHttpClient okHttpClient = new OkHttpClient();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, final IOException e) {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
Toast.makeText(getActivity(), getResources().getString(R.string.txt_error_occured), Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
});
}
@Override
public void onResponse(Call call, final okhttp3.Response response) throws IOException {
getActivity().runOnUiThread(new Runnable() {
@Override
public void run() {
try {
ResponseBody responseBody = response.body();
String content = responseBody.string();
Log.e("TAG", "pic upload " + content);
JSONObject jsonObject = new JSONObject(content);
if (jsonObject.has("success")) {
Toast.makeText(getActivity(), "mail sent successfully.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getActivity(), jsonObject.getString("error"), Toast.LENGTH_SHORT).show();
}
description.setText(" ");
txt_picture_preview.setImageResource(android.R.color.transparent);
selecttypedec.setSelection(0);
progressDialog.dismiss();
// Log.d("TAG", "response of image: " + response.body().string());
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
});
}