Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用volley框架在单次调用中使用JSON参数的多部分图像请求_Android_Apache_Android Volley_Multipartform Data_Multipartentity - Fatal编程技术网

Android 使用volley框架在单次调用中使用JSON参数的多部分图像请求

Android 使用volley框架在单次调用中使用JSON参数的多部分图像请求,android,apache,android-volley,multipartform-data,multipartentity,Android,Apache,Android Volley,Multipartform Data,Multipartentity,我正在以截取方式发送多部分Post请求以及JSon参数{asrequired by server},但在服务器端接收到空参数 在这个请求中,我首先需要在一部分发送请求的参数,然后在下一部分发送图像文件 Map<String, String> params = new HashMap<String, String>(); params.put("appkey", Constants.REQUEST_API_KEY); para

我正在以截取方式发送多部分Post请求以及JSon参数{asrequired by server},但在服务器端接收到空参数

在这个请求中,我首先需要在一部分发送请求的参数,然后在下一部分发送图像文件

Map<String, String> params = new HashMap<String, String>();
            params.put("appkey", Constants.REQUEST_API_KEY);
            params.put("LoginID",issueRequest.getUserName());
            params.put("device_id", issueRequest.getImei().toString());
            params.put("tokenID", AppSharedPreferance.getAppSharedPreferanceInstance(mContext).getToken(Constants.REQUEST_TOKEN, null));
            params.put("issueId", String.valueOf(mRequestIssueId));
            params.put("serverID", String.valueOf(mServerId));

        JSONObject requestObj = new JSONObject();
        requestObj.put("ISSUE_DATA_KEY", new JSONObject(params));
我的截击请求类:

public  class PhotoMultipartRequest<T> extends Request<T> {


private static final String FILE_PART_NAME = "file";
private static final String FILE_JSON_PART_NAME = "parms";

private MultipartEntityBuilder mBuilder = MultipartEntityBuilder.create();
private final Response.Listener<T> mListener;
private final File mImageFile;
protected Map<String, String> headers;
protected String params;
protected static final String PROTOCOL_CHARSET = "utf-8";

public PhotoMultipartRequest(String url, ErrorListener errorListener, Listener<T> listener, File imageFile){
    super(Method.POST, url, errorListener);

    mListener = listener;
    mImageFile = imageFile;

    buildMultipartEntity();
}

public PhotoMultipartRequest(String url,String params, ErrorListener errorListener, Listener<T> listener, File imageFile){
    super(Method.POST, url, errorListener);

    mListener = listener;
    mImageFile = imageFile;
    this.params=params;

    try {
        buildMultipartEntity1();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = super.getHeaders();

    if (headers == null
            || headers.equals(Collections.emptyMap())) {
        headers = new HashMap<String, String>();
    }

    headers.put("Accept", "application/json");

    return headers;
}

private void buildMultipartEntity1() throws UnsupportedEncodingException {

    mBuilder.addPart(FILE_JSON_PART_NAME, new StringBody(params));

    mBuilder.addBinaryBody(FILE_PART_NAME, mImageFile, ContentType.create("image/jpeg"), mImageFile.getName());
    mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    mBuilder.setLaxMode().setBoundary("xx").setCharset(Charset.forName("UTF-8"));

}

private void buildMultipartEntity(){


    mBuilder.addBinaryBody(FILE_PART_NAME, mImageFile, ContentType.create("image/jpeg"), mImageFile.getName());
    mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    mBuilder.setLaxMode().setBoundary("xx").setCharset(Charset.forName("UTF-8"));

}

@Override
public String getBodyContentType(){
    String contentTypeHeader = mBuilder.build().getContentType().getValue();
    return contentTypeHeader;
}

@Override
public byte[] getBody() throws AuthFailureError {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        mBuilder.build().writeTo(bos);
    } catch (IOException e) {
        VolleyLog.e("IOException writing to ByteArrayOutputStream bos, building the multipart request.");
    }

    return bos.toByteArray();
}

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response)
{
    try {
        String result = null;
        result = new String( response.data, HttpHeaderParser.parseCharset( response.headers ) );
        return ( Response<T> ) Response.success( new JSONObject( result ), HttpHeaderParser.parseCacheHeaders(response) );
    } catch ( UnsupportedEncodingException e ) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}

@Override
protected void deliverResponse(T response) {
    mListener.onResponse(response);
}
公共类光电倍增管请求扩展请求{
私有静态最终字符串文件\u PART\u NAME=“FILE”;
私有静态最终字符串文件\u JSON\u PART\u NAME=“parms”;
私有MultipartEntityBuilder mBuilder=MultipartEntityBuilder.create();
私人最终回应。监听器;
私有最终文件mImageFile;
受保护的地图标题;
受保护的字符串参数;
受保护的静态最终字符串协议\u CHARSET=“utf-8”;
公共光电倍增部分请求(字符串url、ErrorListener ErrorListener、Listener Listener、文件imageFile){
super(Method.POST、url、errorListener);
mListener=监听器;
mImageFile=imageFile;
buildMultipartEntity();
}
公共光电倍增部分请求(字符串url、字符串参数、ErrorListener ErrorListener、Listener Listener、文件imageFile){
super(Method.POST、url、errorListener);
mListener=监听器;
mImageFile=imageFile;
this.params=params;
试一试{
buildMultipartEntity1();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map headers=super.getHeaders();
if(headers==null
||headers.equals(Collections.emptyMap()){
headers=newhashmap();
}
headers.put(“接受”、“应用程序/json”);
返回标题;
}
私有void buildMultipartEntity1()引发不受支持的编码异常{
addPart(文件名,新的StringBody(参数));
mBuilder.addBinaryBody(文件名、mImageFile、ContentType.create(“image/jpeg”)、mImageFile.getName();
mBuilder.setMode(HttpMultipartMode.BROWSER_兼容);
mBuilder.setLaxMode().setBoundary(“xx”).setCharset(Charset.forName(“UTF-8”));
}
私有void buildMultipartEntity(){
mBuilder.addBinaryBody(文件名、mImageFile、ContentType.create(“image/jpeg”)、mImageFile.getName();
mBuilder.setMode(HttpMultipartMode.BROWSER_兼容);
mBuilder.setLaxMode().setBoundary(“xx”).setCharset(Charset.forName(“UTF-8”));
}
@凌驾
公共字符串getBodyContentType(){
字符串contentTypeHeader=mBuilder.build().getContentType().getValue();
返回contentTypeHeader;
}
@凌驾
公共字节[]getBody()抛出AuthFailureError{
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
试一试{
mBuilder.build().writeTo(bos);
}捕获(IOE异常){
e(“IOException写入ByteArrayOutputStream bos,构建多部分请求”);
}
返回bos.toByteArray();
}
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应)
{
试一试{
字符串结果=null;
结果=新字符串(response.data,HttpHeaderParser.parseCharset(response.headers));
return(Response)Response.success(新的JSONObject(result)、HttpHeaderParser.parseCacheHeaders(Response));
}捕获(不支持的编码异常e){
返回Response.error(新的ParseError(e));
}捕获(JSONException je){
返回Response.error(newparseerror(je));
}
}
@凌驾
受保护的无效交付响应(T响应){
mListener.onResponse(response);
}
}


我找不到任何错误的线索。那么,有人能帮我更正我的请求类吗?

发布我无法访问的服务器端代码plzIt:(有人能检查一下吗?)我想你可以尝试替换
mBuilder.addPart(FILE\u JSON\u PART\u NAME,new StringBody(params));
以下示例代码
JSONObject jsonBody=new JSONObject();jsonBody.put(“Description”,“测试描述”);jsonBody.put(“作者”,“测试作者”);jsonBody.put(“日期”,“2015/08/21”);mBuilder.addTextBody(文件名,jsonBody.toString(),ContentType.create(“应用程序/JSON”);
@BNK我已经尝试过了,但它不起作用。
{"response_string":"Invalid request.","error_code":"1","required_params":null}
public  class PhotoMultipartRequest<T> extends Request<T> {


private static final String FILE_PART_NAME = "file";
private static final String FILE_JSON_PART_NAME = "parms";

private MultipartEntityBuilder mBuilder = MultipartEntityBuilder.create();
private final Response.Listener<T> mListener;
private final File mImageFile;
protected Map<String, String> headers;
protected String params;
protected static final String PROTOCOL_CHARSET = "utf-8";

public PhotoMultipartRequest(String url, ErrorListener errorListener, Listener<T> listener, File imageFile){
    super(Method.POST, url, errorListener);

    mListener = listener;
    mImageFile = imageFile;

    buildMultipartEntity();
}

public PhotoMultipartRequest(String url,String params, ErrorListener errorListener, Listener<T> listener, File imageFile){
    super(Method.POST, url, errorListener);

    mListener = listener;
    mImageFile = imageFile;
    this.params=params;

    try {
        buildMultipartEntity1();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = super.getHeaders();

    if (headers == null
            || headers.equals(Collections.emptyMap())) {
        headers = new HashMap<String, String>();
    }

    headers.put("Accept", "application/json");

    return headers;
}

private void buildMultipartEntity1() throws UnsupportedEncodingException {

    mBuilder.addPart(FILE_JSON_PART_NAME, new StringBody(params));

    mBuilder.addBinaryBody(FILE_PART_NAME, mImageFile, ContentType.create("image/jpeg"), mImageFile.getName());
    mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    mBuilder.setLaxMode().setBoundary("xx").setCharset(Charset.forName("UTF-8"));

}

private void buildMultipartEntity(){


    mBuilder.addBinaryBody(FILE_PART_NAME, mImageFile, ContentType.create("image/jpeg"), mImageFile.getName());
    mBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
    mBuilder.setLaxMode().setBoundary("xx").setCharset(Charset.forName("UTF-8"));

}

@Override
public String getBodyContentType(){
    String contentTypeHeader = mBuilder.build().getContentType().getValue();
    return contentTypeHeader;
}

@Override
public byte[] getBody() throws AuthFailureError {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    try {
        mBuilder.build().writeTo(bos);
    } catch (IOException e) {
        VolleyLog.e("IOException writing to ByteArrayOutputStream bos, building the multipart request.");
    }

    return bos.toByteArray();
}

@Override
protected Response<T> parseNetworkResponse(NetworkResponse response)
{
    try {
        String result = null;
        result = new String( response.data, HttpHeaderParser.parseCharset( response.headers ) );
        return ( Response<T> ) Response.success( new JSONObject( result ), HttpHeaderParser.parseCacheHeaders(response) );
    } catch ( UnsupportedEncodingException e ) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}

@Override
protected void deliverResponse(T response) {
    mListener.onResponse(response);
}