我现在在共享首选项中存储了两个值,我想将图像文件发送到服务器,并且ID存储在共享首选项中,现在我想在多部分类中获得访问权限,请帮助我。我也尝试添加上下文,但对我不起作用。作为响应,我之前在共享首选项中存储了 id,现在我想要这个类中的 n,所以我想在这里检索它并将其作为 param 在凌空请求中传递。
public class MultiPartRequest extends Request<String> {
private Response.Listener<String> mListener;
private HttpEntity mHttpEntity;
public MultiPartRequest(Response.ErrorListener errorListener, Response.Listener listener, ArrayList<File> file, int numberOfFiles) {
super(Method.POST, EndPointAPI.PATH, errorListener);
mListener = listener;
mHttpEntity = buildMultipartEntity(file, numberOfFiles);
}
private HttpEntity buildMultipartEntity(ArrayList<File> file, int numberOffiles) {
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
for (int i = 0; i < file.size(); i++) {
SharedPreferences prefs1 = getSharedPreferences("ABC", MODE_PRIVATE);
i have tried
SharedPreferences prefs1 = mcontext.getSharedPreferences("ABC", Context.MODE_PRIVATE);
String token = prefs1.getString("token", "");
String aksk_no = prefs1.getString("aksk_no", "");
Date c = Calendar.getInstance().getTime();
String strDateFormat = "MM";
SimpleDateFormat sdf = new SimpleDateFormat(strDateFormat);
String strMonth = sdf.format(c);
SimpleDateFormat df = new SimpleDateFormat("MMM/yyyy");
String formattedDate = df.format(c);
String[] separated = formattedDate.split("/");
String strYear = separated[1];
FileBody fileBody = new FileBody(file.get(i));
builder.addPart("fileToUpload", fileBody);
builder.addTextBody("a_k_s_no", aksk_no.trim();
builder.addTextBody("year", strYear);
builder.addTextBody("month", strMonth);
builder.addTextBody("token", token.trim();
}
builder.addTextBody(Template.Query.KEY_DIRECTORY, Template.Query.VALUE_DIRECTORY);
builder.addTextBody("numberOfFiles", String.valueOf(numberOffiles));
return builder.build();
}@Override
public String getBodyContentType() {
return mHttpEntity.getContentType().getValue();
}
@Override
public byte[] getBody() throws AuthFailureError {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try {
mHttpEntity.writeTo(bos);
return bos.toByteArray();
} catch (IOException e) {
VolleyLog.e("" + e);
return null;
} catch (OutOfMemoryError e) {
VolleyLog.e("" + e);
return null;
}
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
try {
return Response.success(new String(response.data, "UTF-8"),
getCacheEntry());
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
return Response.success(new String(response.data),
getCacheEntry());
}
}
@Override
protected void deliverResponse(String response) {
mListener.onResponse(response);
}
}
你喜欢这样,
Context context;
public MultiPartRequest(Response.ErrorListener errorListener, Response.Listener listener, ArrayList<File> file, int numberOfFiles,Context context){
this.context = context;
}
SharedPreferences prefs1 = context.getSharedPreferences("ABC", Context.MODE_PRIVATE);