当前位置:编程学习 > JAVA >>

获取的输出流转为string后中文是乱码。

package com.test;

import java.io.IOException;

import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.NameValuePair;
import org.apache.commons.httpclient.methods.PostMethod;
import org.apache.commons.httpclient.params.HttpMethodParams;

import android.app.Activity;
import android.os.Bundle;

public class LibActivity extends Activity {
    /** Called when the activity is first created. */
String html = null;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        HttpClient client = new HttpClient();
        client.getParams().setParameter( 
               HttpMethodParams.HTTP_CONTENT_CHARSET, "GBK"); 
// Stream页面里面有Host地址 端口是80
client.getHostConfiguration().setHost("http://coin.lib.scuec.edu.cn",
80);
// 用目标地址 实例一个POST方法
PostMethod post = new PostMethod(
"http://coin.lib.scuec.edu.cn/cgi-bin/IlaswebBib");
// 将需要的键值对写出来
NameValuePair beg = new NameValuePair("FLD_DAT_BEG", "");
NameValuePair end = new NameValuePair("FLD_DAT_END", "");
NameValuePair submit = new NameValuePair("submit", "查 询");
NameValuePair vIndex = new NameValuePair("v_index", "TITLE");
NameValuePair vLogicSrch = new NameValuePair("v_LogicSrch", "0");
NameValuePair vPagenum = new NameValuePair("v_pagenum", "10");
NameValuePair vSeldatabase = new NameValuePair("v_seldatabase", "0");
NameValuePair vValue = new NameValuePair("v_value", "android开发");
String condition="android开发";
//NameValuePair vValue=null;
/*try {
vValue = new NameValuePair("v_value", new String(condition.getBytes(),"ISO-8859-1"));
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
*/
// 给POST方法加入上述键值对
post.setRequestBody(new NameValuePair[] { beg, end, submit, vIndex,
vLogicSrch, vPagenum, vSeldatabase, vValue });
// 执行POST方法
try {
client.executeMethod(post);
} catch (HttpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 将POST返回的数据以流的形式读入,再把输入流流至一个buff缓冲字节数组
// StreamTool类是我自己写的一个工具类,其内容将在下文附出
byte[] buff;
try {
buff = StreamTool.readInputStream(post.getResponseBodyAsStream());
html = new String(buff);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// 将返回的内容格式化为String存在html中

// 任务完成了,释放连接
post.releaseConnection();
System.out.println("html---->"+html);

    }
  
}

package com.test;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;

//StreamTool类如下
public class StreamTool {
        /**
         * 从输入流中获取数据
         * @param inputStream 输入流
         * @return 字节数组
         * @throws Exception
         */
        public static byte[] readInputStream(InputStream inputStream) throws Exception
        {
//实例化一个输出流
        
                ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
//一个1024字节的缓冲字节数组
                byte[] buffer = new byte[1024];
                int len = 0;
//读流的基本知识
                while ((len=inputStream.read(buffer)) != -1) {
                        outputStream.write(buffer, 0, len);
                }
//用完要关,大家都懂的
                inputStream.close();
                return outputStream.toByteArray();
        }
}


最后输出的html内容,中文是乱码?请问是流的处理的问题吗?怎么解决呢? --------------------编程问答-------------------- 估计是字符没有设置正确. --------------------编程问答-------------------- 额,怎么设置? --------------------编程问答-------------------- 首先建议你StreamTool.readInputStream方法里面使用InputStreamReader然后BufferedReader来读取输入流.
另外如果你必须用你现在的方式的话, 在 html = new String(buff); 这个部分给string加上字符编码. 
具体什么编码按照对方的编码进行设置. --------------------编程问答-------------------- 试试不要用ByteArrayOutputStream因为它是输出一个字节的,gbk的中文2个字节
用CharArrayWriter试试
补充:Java ,  Java相关
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,