Java solution: the return value is the url address with Chinese parameters (the phone cannot be accessed, the pc is normal)

The url address of the get request is connected to the solution containing Chinese

problem

1. In a recent project, there is a requirement to turn the acquired parameters into an interface access link for accessing external systems, and it is accessed on the mobile phone, but because the mobile phone cannot recognize the type of Chinese parameters, such as the following format connection: http:// 112.74.194.118:8088/bookManage/xdWeixin/form.html?dept_id=191&dept_name=Chinese Chinese&item_id=356&item_name=Chinese Chinese Chinese&_t=1590483360989
2. In addition, other project interfaces also have restrictions on parameters (order and number are defined), Therefore, it is necessary to meet the interface sequence parameter requirements

Solutions

The url with Chinese parameters is escaped into encodeURI encoding format, that is, utf-8 encoding type; for sequence parameters, LinkedHashMap is used to save the order of key values.

solution

Write a Java method for defining the dead part of the interface and the active parameter params, the code is as follows:

/**
 * 根据参数列表和父路径生成可访问的url连接
 * @param p_url 接口父路径
 * @param params 参数列表
 * @return
 * @throws UnsupportedEncodingException
 */
private String ecodeUrlParams2Utf8(String p_url, LinkedHashMap<String, Object> params) throws UnsupportedEncodingException {
    
    
     StringBuilder url = new StringBuilder(p_url);
     //判断当前传入的父路径url是否含有‘?’结尾
     if (url.indexOf("?") < 0) {
    
    
         url.append('?');
     }
     Set<String> keys =  params.keySet();
     for (String name : keys) {
    
    
         url.append('&');
         url.append(name);
         url.append('=');
         //做URLEncoder处理
         url.append(URLEncoder.encode(String.valueOf(params.get(name)), "UTF-8"));
     }
     return url.toString().replace( "?&", "?");
}

The above solves the problem of Chinese parameter url order transmission, and I
hope you all can encourage you. . . .

Guess you like

Origin blog.csdn.net/rao991207823/article/details/106372566