无论用什么方法,,都是乱码

无论用什么方法,,都是乱码

11-06 20:13:44.964 17265-18249/com.example.analyzesm.httpdemo I/DetailActivity: initData: �����������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������


正在回答 回答被采纳积分+1

登陆购买课程后可参与讨论,去登陆

3回答
好帮手慕雪 2017-11-07 11:01:39

你的in ,baos,还有b没有任何关联呀。请参考

InputStream in = conn.getInputStream();  
BufferedReader buffer = new BufferedReader(new InputStreamReader(in));  
StringBuffer bs = new StringBuffer();  
String str = null;  
while((l=buffer.readLine())!=null){  
     bs.append(str);  
}  
 Log.d(TAG,bs.toString());

祝:学习愉快

提问者 夏析 2017-11-07 07:57:15

String v = new String(baos.toByteArray(),"utf8");
String v1 = new String(baos.toByteArray(),"gbk");


也都是乱码  

提问者 夏析 2017-11-06 20:44:18

new Thread(){
   @Override
   public void run() {
       super.run();
       try {
           URL url = new URL("http://www.imooc.com/api/teacher?type=3&cid=1");
           HttpURLConnection conn = (HttpURLConnection) url.openConnection();
           conn.setConnectTimeout(6000);
           conn.setRequestMethod("GET");
           int Rscode = conn.getResponseCode();

           if(Rscode == 200){
               InputStream in = conn.getInputStream();
               byte[] b = new byte[1024*512];
               int len;
               ByteArrayOutputStream baos = new ByteArrayOutputStream();
               while ((len = in.read())!=-1){
                   baos.write(b,0,len);
               }
               byte[] s = baos.toByteArray();
               Log.i(TAG, "initData: "+new String(s,"utf-8"));
           }
       } catch (MalformedURLException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
   }
}.start();

  • 同学你好,我跟你的问题一样,老师回答的方向不对,你只是代码中while ((len = in.read())!=-1){ baos.write(b,0,len); }while循环条件没有写上数组b>>>in.read(b)
    2017-11-09 16:05:42
问题已解决,确定采纳
还有疑问,暂不采纳

恭喜解决一个难题,获得1积分~

来为老师/同学的回答评分吧

0 星
Android网络操作与数据存储2018版
  • 参与学习           人
  • 提交作业       307    份
  • 解答问题       1613    个

本专题是联网及数据处理的必备技能。课程从网络基础知识到线程间协同工作、异步下载处理。介绍了Android内外部文件存储、轻量级数据库SQLite的使用。利用屏幕适配、状态保持、百度地图解决实际问题。

了解课程
请稍等 ...
意见反馈 帮助中心 APP下载
官方微信

在线咨询

领取优惠

免费试听

领取大纲

扫描二维码,添加
你的专属老师