getParams未在StringRequest Android中调用

getParams未在StringRequest Android中调用,android,android-volley,Android,Android Volley,我正在使用StringRequest将文件发送到服务器。我正在使用以下代码: final MultipartEntityBuilder mHttpEntity = buildMultipartEntity(files_to_upload, params); Response.Listener<String> rListner = new Response.Listener<String>() { @Override

我正在使用
StringRequest
文件发送到服务器。我正在使用以下代码:

final MultipartEntityBuilder mHttpEntity = buildMultipartEntity(files_to_upload, params);

        Response.Listener<String> rListner = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if(response != null) {
                    Intent intent = new Intent(Constants.ACTION_RESPONSE_RECEIVED);
                    intent.putExtra(Constants.RESPONSE, response);
                    intent.putExtra(SignupActivity.EXTRA_ACTION_RESPONSE, SignupActivity.EXTRA_SIGNUP_DATA);
                    LocalBroadcastManager.getInstance(MyApplication.getContext()).sendBroadcast(intent);
                }
            }
        };

        Response.ErrorListener errorListner = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Intent intent = new Intent(Constants.ACTION_RESPONSE_RECEIVED);
                LocalBroadcastManager.getInstance(MyApplication.getContext()).sendBroadcast(intent);
                if(error != null && error.getMessage() != null) {
//                    Toast.makeText(MyApplication.getContext(), error.getMessage(), Toast.LENGTH_SHORT).show();
                }
                else {
                    Log.i(TAG, "postRequestToServer: onErrorResponse : error message null");
                }
            }
        };

        StringRequest jsonObjectRequest = new StringRequest(Request.Method.POST, url, rListner, errorListner)
        {
            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                return params;
            }

//            @Override
//            public Map<String, String> getHeaders() throws AuthFailureError {
//                return params;
//            }

            @Override
            public String getBodyContentType() {
                return mHttpEntity.build().getContentType().getValue();
            }
//
            @Override
            public byte[] getBody() throws AuthFailureError {
                ByteArrayOutputStream bos = new ByteArrayOutputStream();
                try {
                    mHttpEntity.build().writeTo(bos);
                } catch (IOException e) {
                    VolleyLog.e("IOException writing to ByteArrayOutputStream");
                }
                return bos.toByteArray();
            }
        };

private MultipartEntityBuilder buildMultipartEntity(String files_to_upload, HashMap<String, String> params) {

        MultipartEntityBuilder builder = MultipartEntityBuilder.create();

        String[] arr_files = files_to_upload.split("##");
        for(int i = 0; i < arr_files.length; i++) {
            String filePath = arr_files[i];
            if(filePath == null || filePath.length() == 0)
                continue;
            File file = new File(filePath);
            String extension = MimeTypeMap.getFileExtensionFromUrl(arr_files[i]);
            String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);

            builder.addBinaryBody("userfile", file, ContentType.create(mimeType), file.getName());
//            builder.addPart("userfile", new FileBody(file));
        }

        try {
            for (String key: params.keySet())
                builder.addPart(key, new StringBody(params.get(key)));
        } catch (UnsupportedEncodingException e) {
            VolleyLog.e("UnsupportedEncodingException");
        }

        return builder;
    }
final MultipartEntityBuilder mhtpentity=buildMultipartEntity(文件上传到上传,参数);
Response.Listener rListner=新的Response.Listener(){
@凌驾
公共void onResponse(字符串响应){
if(响应!=null){
意向意向=新意向(收到常量、动作和响应);
intent.putExtra(常量.RESPONSE,RESPONSE);
intent.putExtra(SignupActivity.EXTRA\u ACTION\u RESPONSE,SignupActivity.EXTRA\u SIGNUP\u DATA);
LocalBroadcastManager.getInstance(MyApplication.getContext()).sendBroadcast(intent);
}
}
};
Response.ErrorListener errorListner=新的Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
意向意向=新意向(收到常量、动作和响应);
LocalBroadcastManager.getInstance(MyApplication.getContext()).sendBroadcast(intent);
if(error!=null&&error.getMessage()!=null){
//Toast.makeText(MyApplication.getContext(),error.getMessage(),Toast.LENGTH_SHORT).show();
}
否则{
i(标记“postRequestToServer:onErrorResponse:error message null”);
}
}
};
StringRequest jsonObjectRequest=新的StringRequest(Request.Method.POST、url、rListner、errorListner)
{
@凌驾
受保护的映射getParams()引发AuthFailureError{
返回参数;
}
//@覆盖
//公共映射getHeaders()引发AuthFailureError{
//返回参数;
//            }
@凌驾
公共字符串getBodyContentType(){
返回mhtpentity.build().getContentType().getValue();
}
//
@凌驾
公共字节[]getBody()抛出AuthFailureError{
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
试一试{
mhtpentity.build().writeTo(bos);
}捕获(IOE异常){
e(“向ByteArrayOutputStream写入IOException”);
}
返回bos.toByteArray();
}
};
私有MultipartEntityBuilder构建MultipartEntity(字符串文件到上传,HashMap参数){
MultipartEntityBuilder=MultipartEntityBuilder.create();
字符串[]arr_files=文件到上传。拆分(“###”);
对于(int i=0;i
但是问题是没有调用
getParams
。服务器需要参数,我尝试使用
EntityBuilder
发送,但发送参数时仍有错误

任何人都可以让我知道我如何上传文件使用
StringRequest和参数

由于StringRequest.java继承自Request.java,因此未调用getParams()。现在在Request.java中,如果查看getBody()方法

现在通过这个类发送您的请求。不要重写getParams(),只需为参数创建一个hashmap,并将其传递到构造函数中即可

未调用getParams(),因为StringRequest.java继承自Request.java。现在在Request.java中,如果查看getBody()方法


现在通过这个类发送您的请求。不要重写getParams(),只需为参数创建一个hashmap,并将其传递到构造函数中即可

使用
getparams
getHeader
方法:

@Override
protected Map<String, String> getParams() throws AuthFailureError {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("product_id", "4");
    parameters.put("count", Productcount.getText().toString());
    parameters.put("type", cashstatus);
    parameters.put("description", "Matn bo'ladi");
    parameters.put("phone_number", "946287009");
    parameters.put("on_map", address);
    return parameters;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Authorization", "Bearer " + token);
    return headers;
}
@覆盖
受保护的映射getParams()引发AuthFailureError{
映射参数=新的HashMap();
参数。put(“产品标识”,“4”);
parameters.put(“count”,Productcount.getText().toString());
参数。put(“类型”,现金状态);
参数。put(“说明”、“Matn bo'ladi”);
参数put(“电话号码”,“946287009”);
参数。放置(“在地图上”,地址);
返回参数;
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map headers=newhashmap();
头.put(“授权”、“持有人”+令牌);
返回标题;
}

使用
getparams
getHeader
方法:

@Override
protected Map<String, String> getParams() throws AuthFailureError {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("product_id", "4");
    parameters.put("count", Productcount.getText().toString());
    parameters.put("type", cashstatus);
    parameters.put("description", "Matn bo'ladi");
    parameters.put("phone_number", "946287009");
    parameters.put("on_map", address);
    return parameters;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Authorization", "Bearer " + token);
    return headers;
}
@覆盖
受保护的映射getParams()引发AuthFailureError{
映射参数=新的HashMap();
参数。put(“产品标识”,“4”);
parameters.put(“count”,Productcount.getText().toString());
参数。put(“类型”,现金状态);
参数。put(“说明”、“Matn bo'ladi”);
参数put(“电话号码”,“946287009”);
参数。放置(“在地图上”,地址);
返回参数;
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map headers=newhashmap();
头.put(“授权”、“持有人”+令牌);
返回标题;
}

据我所知,您的问题::当您使用凌空截击发出
StringRequest
时,您正在发出
@Override
protected Map<String, String> getParams() throws AuthFailureError {
    Map<String, String> parameters = new HashMap<String, String>();
    parameters.put("product_id", "4");
    parameters.put("count", Productcount.getText().toString());
    parameters.put("type", cashstatus);
    parameters.put("description", "Matn bo'ladi");
    parameters.put("phone_number", "946287009");
    parameters.put("on_map", address);
    return parameters;
}

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<String, String>();
    headers.put("Authorization", "Bearer " + token);
    return headers;
}