bcpiaoli 2016-02-21 15:40 采纳率: 66.7%
浏览 3270
已采纳

android客户端解析json,出现乱码 ( 没有中文 ) ,请大神指点

本人使用Tomcat apache-tomcat-8.0.28虚拟服务器,默认utf-8编码

update.json

{"versionName":"2.0","versionCode":2,"description":"Hello word!!","downloadUrl":"http://www.baidu.com"}

android studio 1.3.1 做了个客户端:部分编码如下:

private void checkVersion(){
new Thread(){
@Override
public void run() {

            try {
                URL url = new URL("http://10.0.2.2:8080/update.json");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.setRequestMethod("GET");
                conn.setConnectTimeout(5000);
                conn.setReadTimeout(5000);
                conn.connect();

                int responseCode = conn.getResponseCode();

                if(responseCode==200){
                    InputStream inputStream = conn.getInputStream();
                    String result = StreamUtils.readFromSteam(inputStream);
                    Log.e("System.Out.Print", "result:"+result);

                    //解析Json

                    JSONObject jo = new JSONObject(result);
                    mVersionName = jo.getString("versionName");
                    mVersionCode = jo.getInt("versionCode");
                    mDescription = jo.getString("description");
                    mDownloadUrl = jo.getString("downloadUrl");


                    Log.e("System.Out.Print", "run "+mVersionName+";"+mVersionCode);
                    Log.e("System.Out.Print", "run "+mDescription+";"+mDownloadUrl);
                }

            } catch (MalformedURLException e) {
               // utl错误的异常

                e.printStackTrace();
            } catch (IOException e) {
                //网络错误的异常

                e.printStackTrace();
            }
            catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }.start();
}

public class StreamUtils {

/**
 * readFromSteam() 将输入流读成String后返回
 * @return
 */
public static String readFromSteam(InputStream in) throws IOException {

    ByteArrayOutputStream out = new ByteArrayOutputStream();
    int len = 0;
    byte[] buffer = new byte[1024];

    while((len=in.read())!=-1){
        out.write(buffer,0,len);
    }
        String result = out.toString();
        in.close();
        out.close();
    return result;
    }

}


    每次运行后的结果如下:

    ![图片说明](https://img-ask.csdn.net/upload/201602/21/1456069188_115538.jpg)


    请大神指点
  • 写回答

6条回答 默认 最新

  • a999888huang 2016-02-21 16:14
    关注

    在用HttpURLConnection的时候,没有设置默认编码,可能是utf-8和 "ISO-8859-1"混淆了。
    然后模拟器和手机不一样的原因是;Android版本不一样导致HttpURLConnection的具体实现不一样,记得Android4.4之后,HttpURLConnection底层采用HttpOK了。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作