URLConnection中url参数为中文的处理

使用URLConnection进行http连接的时候会遇到参数为中文的情况,这个时候需要使用URLEncoder.encode把参数部分单独处理一下再拼接到url上:

String num=URLEncoder.encode(number,"UTF-8");
			    				
String url="http://xx.xx.xx.xx:xx/getTraffic?lineID="+num;
URLConnection connection = null;
StringBuffer buffer = new StringBuffer();
	 BufferedReader br=null;	 		
	 try {
	       connection = new URL(url).openConnection();
	       connection.connect();
	
	       InputStream fin = connection.getInputStream();
	       br = new BufferedReader(new InputStreamReader(fin,"UTF-8"));
            
	       String temp = null;
	       while ((temp = br.readLine()) != null) {
	                buffer.append(temp);
	            }
	            	            
	 }catch (IOException e){
	 			
	}


你可能感兴趣的:(URLConnection中url参数为中文的处理)