URL转码

最近在做一个请求天气数据的功能,发现其实很多接口都是可以输入中文的,但是在输入中文的后,请求数据却是铁定的不会成功。研究了一下后知道了原因,原来是在URL地址中所谓的汉字最后都做了转码,将汉字转成一群乱码,然后发送至服务器请求json或者xml数据。废话不多说,上源码:

package com.hisign.csipadmini_free.biz;



import java.io.UnsupportedEncodingException;
/**
 * 描述:url解码,转码
 * 公司:
 * 作者:octopus
 * 创建时间 2016/10/18
 */
public class UrlUtil { private final static String ENCODE = "UTF-8"; /** * URL 解码 * @param str * @return */ public static String getURLDecoderString(String str) { String result = ""; if (null == str) { return ""; } try { result = java.net.URLDecoder.decode(str, ENCODE); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; } /** * URL 转码 * @param str * @return */ public static String getURLEncoderString(String str) { String result = ""; if (null == str) { return ""; } try { result = java.net.URLEncoder.encode(str, ENCODE); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return result; }}

接下来只要将其拼入URL地址中就好了,

**
 * 按城市名请求,温度
 * @param cityName
 */
public void requestTemperatureWithHttpClient(String cityName){
    if (cityName.endsWith(getResourceStr(R.string.str_char_city))){
        cityName = cityName.substring(0,cityName.length()-1);
    }
    StringBuilder stringBuilder = new StringBuilder("http://api.map.baidu.com/telematics/v3/weather?location=")
        .append(getURLEncoderString(cityName))
        .append("&output=json&ak=").append("nidebaiduMap apikey").append("&mcode=你的Mcode");
    sendRequest(stringBuilder.toString());
}	
private void sendRequest(final String urlstr) {
        new Thread(new Runnable() {
            @Override
            public void run() {
                HttpURLConnection connection = null;
                try {
                    URL url = new URL(urlstr);
                    connection = (HttpURLConnection) url.openConnection();
                    connection.setRequestMethod("GET");
                    connection.setConnectTimeout(5000);
                    connection.setReadTimeout(5000);
                    InputStream in = connection.getInputStream();
                    BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                    StringBuilder response = new StringBuilder();
                    String line;
                    while ((line = reader.readLine()) != null) {
                        response.append(line);
                    }
//                    Log.i("TAG", response.toString());
                    parseJSONToWeather(response.toString());
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }
	再下来就是解析接收到的json字符串了,小菜一碟,祝好!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值