冷静的油条 · pytest自动发送测试报告邮件 ...· 5 天前 · |
干练的稀饭 · 4-生死相依 - 我独自成神 - 包子漫画· 1 年前 · |
玉树临风的野马 · 年少只知多巴胺,中年才懂内啡肽 - 知乎· 1 年前 · |
慷慨大方的镜子 · 高斯过程回归_抖抖音· 1 年前 · |
千杯不醉的红烧肉 · 【南瓜学堂】股市涨了,又跌了,在抄底前这些书 ...· 1 年前 · |
不拘小节的太阳 · 雷诺日产三菱联盟研发全新电动车平台_搜狐汽车 ...· 1 年前 · |
我正试图使用我的安卓应用程序的PUT请求将一个文件上传到android。我从服务器上得到了一个链接,其中的私钥也暴露在这个链接中。
当试图从邮递员上传文件时,它在上传时使用二进制选项和内容类型应用程序/八进制流或application/x form -urlencoded(两者都尝试过),但是如果使用表单数据上传相同的图像,它就不能工作。
在我的安卓应用程序中,我尝试过使用我正在获得的JsonObject请求。
Json Org异常
请在 下面找到我的请求
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.PUT,
url, new JSONObject(parameters),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
if (listener != null) {
Log.e("Response", response + "");
listener.onResult(response + "", responseCode);
if (showDialog == true) {
try {
mOverlayDialog.dismiss();
} catch (Exception e) {
// Crashlytics.logException(e);
try {
materialDialog.dismiss(); // To hide the progress bar
} catch (Exception e) {
// Crashlytics.logException(e);
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (listener != null) {
try {
if(error.networkResponse!=null && error.networkResponse.data!=null) {
String responseBody = new String(error.networkResponse.data, "utf-8");
if (!responseBody.equalsIgnoreCase("")) {
JSONObject data = new JSONObject(responseBody);
listener.onResult(data + "", responseCode);
//listener.onResult(error.toString(), WebAPIConstants.VOLLEY_ERROR);
Log.e("ERROR", error.toString() + "");
} else {
listener.onResult("", responseCode);
listener.onResult("", responseCode);
} catch (JSONException e) {
} catch (UnsupportedEncodingException errorr) {
} catch (Exception e) {
Crashlytics.logException(e);
if (showDialog == true) {
try {
mOverlayDialog.dismiss();
} catch (Exception e) {
// Crashlytics.logException(e);
try {
materialDialog.dismiss(); // To hide the progress bar
} catch (Exception e) {
// Crashlytics.logException(e);
try {
if (error instanceof TimeoutError) {
AlertDialogManager.showAlertDialog(mContext, mContext.getResources().getString(R.string.timeout_error_title), mContext.getResources().getString(R.string.timeout_error_string), new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
} else if (error instanceof NoConnectionError) {
if (CheckInternetConnection.haveNetworkConnection(mContext)) {
AlertDialogManager.showAlertDialog(mContext, mContext.getResources().getString(R.string.timeout_error_title), mContext.getResources().getString(R.string.timeout_error_string), new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
} else {
AlertDialogManager.showAlertDialog(mContext, mContext.getResources().getString(R.string.no_internet_title), mContext.getResources().getString(R.string.no_internet_message), new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
} catch (Exception e) {
Crashlytics.logException(e);
}
(已以字节码发送图像),在使用字符串请求时,正在上载图像,但 大小为1kB,但在服务器 上打开(已以字节码发送图像)。
字符串请求
StringRequest stringRequest=new StringRequest(Request.Method.PUT, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (listener != null) {
Log.e("Response", response + "");
if (showDialog == true) {
try {
mOverlayDialog.dismiss();
} catch (Exception e) {
// Crashlytics.logException(e);
try {
materialDialog.dismiss(); // To hide the progress bar
} catch (Exception e) {
// Crashlytics.logException(e);
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
if (listener != null) {
try {
if(error.networkResponse!=null && error.networkResponse.data!=null) {
String responseBody = new String(error.networkResponse.data, "utf-8");
if (!responseBody.equalsIgnoreCase("")) {
JSONObject data = new JSONObject(responseBody);
listener.onResult(data + "", responseCode);
//listener.onResult(error.toString(), WebAPIConstants.VOLLEY_ERROR);
Log.e("ERROR", error.toString() + "");
} else {
listener.onResult("", responseCode);
listener.onResult("", responseCode);
} catch (JSONException e) {
} catch (UnsupportedEncodingException errorr) {
} catch (Exception e) {
Crashlytics.logException(e);
if (showDialog == true) {
try {
mOverlayDialog.dismiss();
} catch (Exception e) {
// Crashlytics.logException(e);
try {
materialDialog.dismiss(); // To hide the progress bar
} catch (Exception e) {
// Crashlytics.logException(e);
try {
if (error instanceof TimeoutError) {
AlertDialogManager.showAlertDialog(mContext, mContext.getResources().getString(R.string.timeout_error_title), mContext.getResources().getString(R.string.timeout_error_string), new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
} else if (error instanceof NoConnectionError) {
if (CheckInternetConnection.haveNetworkConnection(mContext)) {
AlertDialogManager.showAlertDialog(mContext, mContext.getResources().getString(R.string.timeout_error_title), mContext.getResources().getString(R.string.timeout_error_string), new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
} else {
AlertDialogManager.showAlertDialog(mContext, mContext.getResources().getString(R.string.no_internet_title), mContext.getResources().getString(R.string.no_internet_message), new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
} catch (Exception e) {
Crashlytics.logException(e);
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
if (listener!=null) {
listener.onResult(mStatusCode + "", responseCode);
mStatusCode = response.statusCode;
return super.parseNetworkResponse(response);
@Override
protected Map<String, String> getParams() throws AuthFailureError {
return parameters;
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/octet-stream");
return headers;
stringRequest.setRetryPolicy(new DefaultRetryPolicy(120000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
try {
stringRequest.setShouldCache(false);
queue.getCache().clear();
queue.add(stringRequest);
} catch (Exception e) {
Crashlytics.logException(e);
}
也尝试了多部分请求,但得到了错误,如 、auth、 等。
多部分请求,其中docUrl与postman 中的url相同
为下列请求添加依赖关系:
编译组:'org.apache.httpcomponents‘,名称:'httpclient-android’,版本:'4.3.5.1‘org.apache.httpcomponents{org.apache.httpcomponents模块:"httpclient“}
MultipartRequestAWS类
包装spice.mudra.csp_cred;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkResponse;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyLog;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class MultipartRequestAWS extends Request<String> {
private MultipartEntity entity = new MultipartEntity();
private static final String FILE_PART_NAME = "file";
private static final String STRING_PART_NAME = "text";
private final Response.Listener<String> mListener;
private final File mFilePart;
private final String mStringPart;
public MultipartRequestAWS(String url, Response.Listener<String> listener, Response.ErrorListener errorListener,File file, String stringPart) {
super(Method.POST, url, errorListener);
mListener = listener;
mFilePart = file;
mStringPart = stringPart;
buildMultipartEntity();
private void buildMultipartEntity()
entity.addPart(FILE_PART_NAME, new FileBody(mFilePart));
entity.addPart(STRING_PART_NAME, new StringBody(mStringPart));
catch (UnsupportedEncodingException e)
VolleyLog.e("UnsupportedEncodingException");
@Override
public String getBodyContentType()
return "application/octet-stream";
@Override
public byte[] getBody() throws AuthFailureError
ByteArrayOutputStream bos = new ByteArrayOutputStream();
entity.writeTo(bos);
catch (IOException e)
VolleyLog.e("IOException writing to ByteArrayOutputStream");
return bos.toByteArray();
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response)
return Response.success("Uploaded", getCacheEntry());
@Override
protected void deliverResponse(String response)
mListener.onResponse(response);
}
*多部分请求*
MultipartRequestAWS request = new MultipartRequestAWS(docUrl, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("Responce", "" + response);
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Responce", "" + error);
}, outputFile, imageArray.toString()
request.setRetryPolicy(new DefaultRetryPolicy(300000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
//adding request to request queue
// if(!Constants.IS_PRODUCTION) {
Volley.newRequestQueue(this).add(request);
// }else{
// Volley.newRequestQueue(getActivity(), new OKHttpStack(new URL(request.getUrl()).getHost())).add(request);
// // Volley.newRequestQueue(getActivity(), new HurlStack(null, newSslSocketFactory())).add(request);
// }
其他多部分请求尝试了
private final String boundary = "apiclient-" + System.currentTimeMillis();
private final String mimeType = "multipart/form-data;boundary=" + boundary;
//multipartBody is byte code of image to be uploaded
MultipartRequest request = new MultipartRequest(url, null, mimeType, multipartBody, new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
try {
mOverlayDialogAsync.dismiss();
} catch (Exception e) {
Crashlytics.logException(e);
try {
materialDialogAsync.dismiss(); // To hide the progress bar
} catch (Exception e) {
Crashlytics.logException(e);
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
try {
mOverlayDialogAsync.dismiss();
} catch (Exception e) {
Crashlytics.logException(e);
try {
materialDialogAsync.dismiss(); // To hide the progress bar
} catch (Exception e) {
Crashlytics.logException(e);
try {
// getting json string form the response obtained from server
String jsonString = new String(error.toString());
Log.e("response multipart", jsonString);
} catch (Exception e) {
Crashlytics.logException(e);
try {
//Network errors handling
if (error instanceof TimeoutError) {
AlertDialogManager.showAlertDialog(CustomDialogNetworkRequest.this, getResources().getString(R.string.timeout_error_title), getResources().getString(R.string.timeout_error_string), new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
} else if (error instanceof NoConnectionError) {
if (spice.mudra.utils.CheckInternetConnection.haveNetworkConnection(AddYourProffDetails.this)) {
AlertDialogManager.showAlertDialog(AddYourProffDetails.this, getResources().getString(R.string.timeout_error_title), getResources().getString(R.string.timeout_error_string), new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
} else {
AlertDialogManager.showAlertDialog(AddYourProffDetails.this, getResources().getString(R.string.no_internet_title), getResources().getString(R.string.no_internet_message), new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
} else {
AlertDialogManager.showAlertDialog(AddYourProffDetails.this, "", "Something went wrong. Please try again.", new MaterialDialog.SingleButtonCallback() {
@Override
public void onClick(@NonNull MaterialDialog dialog, @NonNull DialogAction which) {
} catch (Exception e) {
Crashlytics.logException(e);
//setting timeout to multipart request
request.setRetryPolicy(new DefaultRetryPolicy(300000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
//adding request to request queue
Log.i("++++++++++", "Request body: " + new String(request.getBody()));
Volley.newRequestQueue(CustomDialogNetworkRequest.this).add(request);
} catch (Exception e) {
Crashlytics.logException(e);
}
请帮个忙。
请找邮递员截图作为参考。这也有我正在使用的URL类型。
发布于 2021-03-12 05:00:05
要上传图像文件,请向StringRequest对象添加以下函数。这里,outputFileUri是要上传的文件的Uri。s3Url是要放置图像文件的s3桶url。
StringRequest putRequest = new StringRequest(Request.Method.PUT, s3Url,
new Response.Listener<String>()
@Override
public void onResponse(String response) {
// response
new Response.ErrorListener()
@Override
public void onErrorResponse(VolleyError error) {
// error
@Override
public String getBodyContentType() {
return "image/jpeg";
@Override
public byte[] getBody() throws AuthFailureError {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
InputStream inputStream = mContext.getContentResolver().openInputStream(outputFileUri);
byte[] b = new byte[8192];
for (int readNum; (readNum = inputStream.read(b)) != -1; ) {
bos.write(b, 0, readNum);
inputStream.close();
return bos.toByteArray();
} catch (Exception e) {
干练的稀饭 · 4-生死相依 - 我独自成神 - 包子漫画 1 年前 |
玉树临风的野马 · 年少只知多巴胺,中年才懂内啡肽 - 知乎 1 年前 |
慷慨大方的镜子 · 高斯过程回归_抖抖音 1 年前 |