查看了一下是一个js文件,或许这就是乱码的原因,希望有大神可以解答一下。
StockActivity,用于获取,解析并设置显示信息:
package com.xawx.tools.activity;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import com.xawx.tools.R;
import com.xawx.tools.service.ImageDownloadHandler;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
public class StockActivity extends Activity {
/** Called when the activity is first created. */
private Button button;
private EditText editText;
// private static final String baseUrl =
// "http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx/getStockInfoByCode";
// private static final String imageUrl =
// "http://www.webxml.com.cn/WebServices/ChinaStockWebService.asmx/getStockImageByCode";
private static final String baseUrl = "http://hq.sinajs.cn/list=";
private static final String imageUrl = "http://image.sinajs.cn/newchart/min/n/";
private HttpResponse httpResponse = null;
private HttpEntity httpEntity = null;
private ImageView imageView;
public boolean onKeyDown(int kCode, KeyEvent kEvent) {
switch (kCode) {
case KeyEvent.KEYCODE_BACK: {
exitDialog();
return false;
}
}
return super.onKeyDown(kCode, kEvent);
}
public void exitDialog() {
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
String confrimStr = "";
String cancelStr = "";
builder.setTitle("是否确认退出?");
confrimStr = "退出小助手";
cancelStr = "退出股票信息";
builder.setPositiveButton(confrimStr,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
System.exit(0);
}
});
builder.setNeutralButton(cancelStr,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(StockActivity.this,
ToolsMainActivity.class);
StockActivity.this.startActivity(intent);
StockActivity.this.finish();
}
});
builder.create().show();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.stock);
editText = (EditText) findViewById(R.id.stockEditText);
button = (Button) findViewById(R.id.stockButton);
imageView = (ImageView) findViewById(R.id.stockImageView);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// button.performClick();
// TODO Auto-generated method stub
String theStockCode = editText.getText().toString();
String url = baseUrl + theStockCode;
final String urlimage = imageUrl + theStockCode + ".gif";
// 生成一个请求对象
final HttpGet httpGet = new HttpGet(url);
// 生成一个客户端
final HttpClient httpClient = new DefaultHttpClient();
// 使用客户端发送请求
new Thread() {
public void run() {
try {
httpResponse = httpClient.execute(httpGet);
httpEntity = httpResponse.getEntity();
InputStream is = null;
is = httpEntity.getContent();
BufferedReader reader = new BufferedReader(
new InputStreamReader(is));
String result = "";
String line = "";
while ((line = reader.readLine()) != null) {
result = result + line;
}
//本想转换下,但没用
bytes = result.getBytes("UTF-8");
String result1 =new String(bytes,"UTF-8");
List<String> list = new ArrayList<String>();
String[] temp = result1.split(",");
Pattern pattern = Pattern
.compile(".*_([a-z]{2}\\d{6})=\"(.*)$");
Matcher matcher = pattern.matcher(temp[0]);
if (matcher.find()) {
list.add(matcher.group(1));
list.add(matcher.group(2));
}
for (int i = 1; i < temp.length; i++)
list.add(temp[i]);
Bitmap bmp = new ImageDownloadHandler()
.loadImageFromUrl(urlimage);
imageView.setImageBitmap(bmp);
((TextView) findViewById(R.id.stockText2))
.setText("股票代码:" + list.get(0));
((TextView) findViewById(R.id.stockText3))
.setText("股票名称:" + list.get(1));
((TextView) findViewById(R.id.stockText4))
.setText("今日开盘价(元):" + list.get(2));
((TextView) findViewById(R.id.stockText5))
.setText("昨日收盘价(元):" + list.get(3));
((TextView) findViewById(R.id.stockText6))
.setText("当前价格 (元):" + list.get(4));
((TextView) findViewById(R.id.stockText7))
.setText("今日最高价(元):" + list.get(5));
((TextView) findViewById(R.id.stockText8))
.setText("今日最低价(元):" + list.get(6));
((TextView) findViewById(R.id.stockText9))
.setText("竞买价 (元):" + list.get(7));
((TextView) findViewById(R.id.stockText10))
.setText("竞卖价 (元):" + list.get(8));
((TextView) findViewById(R.id.stockText11))
.setText("成交的股票数(股):" + list.get(9));
((TextView) findViewById(R.id.stockText12))
.setText("成交金额 (元):" + list.get(10));
((TextView) findViewById(R.id.stockText13))
.setText("买一(手):" + list.get(11));
((TextView) findViewById(R.id.stockText14))
.setText("买一 (元):" + list.get(12));
((TextView) findViewById(R.id.stockText15))
.setText("买二(手):" + list.get(13));
((TextView) findViewById(R.id.stockText16))
.setText("买二 (元):" + list.get(14));
((TextView) findViewById(R.id.stockText17))
.setText("买三(手):" + list.get(15));
((TextView) findViewById(R.id.stockText18))
.setText("买三 (元):" + list.get(16));
((TextView) findViewById(R.id.stockText19))
.setText("买四(手):" + list.get(17));
((TextView) findViewById(R.id.stockText20))
.setText("买四 (元):" + list.get(18));
((TextView) findViewById(R.id.stockText21))
.setText("买五(手):" + list.get(19));
((TextView) findViewById(R.id.stockText22))
.setText("买五(元):" + list.get(20));
((TextView) findViewById(R.id.stockText23))
.setText("卖一(手):" + list.get(21));
((TextView) findViewById(R.id.stockText24))
.setText("卖一 (元):" + list.get(22));
((TextView) findViewById(R.id.stockText25))
.setText("卖二(手):" + list.get(23));
((TextView) findViewById(R.id.stockText26))
.setText("卖二 (元):" + list.get(24));
((TextView) findViewById(R.id.stockText27))
.setText("卖三(手):" + list.get(25));
((TextView) findViewById(R.id.stockText28))
.setText("卖三 (元):" + list.get(26));
((TextView) findViewById(R.id.stockText29))
.setText("卖四(手):" + list.get(27));
((TextView) findViewById(R.id.stockText30))
.setText("卖四 (元):" + list.get(28));
((TextView) findViewById(R.id.stockText31))
.setText("卖五(手):" + list.get(29));
((TextView) findViewById(R.id.stockText32))
.setText("卖五(元):" + list.get(30));
((TextView) findViewById(R.id.stockText33))
.setText("日期:" + list.get(31));
((TextView) findViewById(R.id.stockText34))
.setText("时间:" + list.get(32));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}.run();
};
});
}
运行结果:股票名称出现乱码
8 个解决方案
#1
你那转换的地方根本没用。reault这个字符串里已经是乱码了,你转那么俩次跟没转一样,在流那块弄下试试
#2
就是不会弄啊,大神指教指教啊
#3
/**
* 根据url调用web接口
*
* @param url
* 访问地址, 必备参数
* @param method
* 请求方式post还是get, 默认get
* @param params
* 参数列表 post必备, 比如:"name=张三&age=18"
* @param sessionInfo
* 可以保持session, 默认不保持
* @param encoding
* 编码格式, 默认UTF_8
* @param isLine
* 得到的源码是否换行, 默认false
* @return String
*/
public static String requestUrl(String url, String method, String params, String sessionInfo, String encoding,
boolean isLine) {
try {
encoding = (ToolUtil.isBlank(encoding) ? CharEncoding.UTF_8 : encoding);
method = (ToolUtil.isBlank(method) ? Constant.REQUEST_GET : method.toUpperCase());
String mathStr = "mathRandom=" + Math.random();
if (method.equals(Constant.REQUEST_GET)) {
url += (url.indexOf("?") != -1 ? "&" : "?") + mathStr;
if (!ToolUtil.isBlank(params)) {
url += "&" + params;
}
}
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
if (!ToolUtil.isBlank(sessionInfo)) {
connection.setRequestProperty("Cookie", sessionInfo);
}
connection.setRequestMethod(method);
connection.setConnectTimeout(15000);
connection.setReadTimeout(15000);
if (method.equals(Constant.REQUEST_POST)) {
connection.setDoOutput(true);
params = (ToolUtil.isBlank(params) ? mathStr : params + "&" + mathStr);
OutputStream output = connection.getOutputStream();
output.write(params.getBytes(encoding));
output.flush();
output.close();
}
String result = FileUtil.readFile(connection.getInputStream(), encoding, 1, isLine);
connection.disconnect();
return result;
} catch (Exception e) {
log.error("根据url调用web接口", e);
throw new MvcException(e.getMessage());
}
}
public static void main(String[] args) {
Date date = new Date();
System.out.println(requestUrl("http://hq.sinajs.cn/list=sh601006 ", "GET", null, null, "GBK", false));
System.out.println("本次调用web接口耗时" + (new Date().getTime() - date.getTime()) + "毫秒");
}
输出结果:
var hq_str_sh601006="大秦铁路,6.72,6.71,6.71,6.79,6.69,6.71,6.72,12584684,84810589,9700,6.71,165890,6.70,112400,6.69,129200,6.68,96400,6.67,137527,6.72,89100,6.73,205072,6.74,139592,6.75,582303,6.76,2014-05-22,15:04:39,00";
本次调用web接口耗时109毫秒
没有乱码
#4
String result = EntityUtils.toString(httpEntity, "utf-8"); 不用getContent那些步骤就可以获取,至于流里那块来转码还不是很清楚
#5
你那转换的地方根本没用。reault这个字符串里已经是乱码了,你转那么俩次跟没转一样,在流那块弄下试试
就是不会弄啊,大神指教指教啊
我日,弄错了,你这根本就不是utf-8,而是GBK! 你把这块加上编码就可以了BufferedReader reader = new BufferedReader(new InputStreamReader(is, "gbk"));
#6
客户端转换时使用的字符编码要和服务器端的字符编码一致,一般选UTF-8
#7
自定httpclient的时候可以设置编码规则
#8
是UTF8编码么?
#1
你那转换的地方根本没用。reault这个字符串里已经是乱码了,你转那么俩次跟没转一样,在流那块弄下试试
#2
你那转换的地方根本没用。reault这个字符串里已经是乱码了,你转那么俩次跟没转一样,在流那块弄下试试
就是不会弄啊,大神指教指教啊
#3
/**
* 根据url调用web接口
*
* @param url
* 访问地址, 必备参数
* @param method
* 请求方式post还是get, 默认get
* @param params
* 参数列表 post必备, 比如:"name=张三&age=18"
* @param sessionInfo
* 可以保持session, 默认不保持
* @param encoding
* 编码格式, 默认UTF_8
* @param isLine
* 得到的源码是否换行, 默认false
* @return String
*/
public static String requestUrl(String url, String method, String params, String sessionInfo, String encoding,
boolean isLine) {
try {
encoding = (ToolUtil.isBlank(encoding) ? CharEncoding.UTF_8 : encoding);
method = (ToolUtil.isBlank(method) ? Constant.REQUEST_GET : method.toUpperCase());
String mathStr = "mathRandom=" + Math.random();
if (method.equals(Constant.REQUEST_GET)) {
url += (url.indexOf("?") != -1 ? "&" : "?") + mathStr;
if (!ToolUtil.isBlank(params)) {
url += "&" + params;
}
}
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
if (!ToolUtil.isBlank(sessionInfo)) {
connection.setRequestProperty("Cookie", sessionInfo);
}
connection.setRequestMethod(method);
connection.setConnectTimeout(15000);
connection.setReadTimeout(15000);
if (method.equals(Constant.REQUEST_POST)) {
connection.setDoOutput(true);
params = (ToolUtil.isBlank(params) ? mathStr : params + "&" + mathStr);
OutputStream output = connection.getOutputStream();
output.write(params.getBytes(encoding));
output.flush();
output.close();
}
String result = FileUtil.readFile(connection.getInputStream(), encoding, 1, isLine);
connection.disconnect();
return result;
} catch (Exception e) {
log.error("根据url调用web接口", e);
throw new MvcException(e.getMessage());
}
}
public static void main(String[] args) {
Date date = new Date();
System.out.println(requestUrl("http://hq.sinajs.cn/list=sh601006 ", "GET", null, null, "GBK", false));
System.out.println("本次调用web接口耗时" + (new Date().getTime() - date.getTime()) + "毫秒");
}
输出结果:
var hq_str_sh601006="大秦铁路,6.72,6.71,6.71,6.79,6.69,6.71,6.72,12584684,84810589,9700,6.71,165890,6.70,112400,6.69,129200,6.68,96400,6.67,137527,6.72,89100,6.73,205072,6.74,139592,6.75,582303,6.76,2014-05-22,15:04:39,00";
本次调用web接口耗时109毫秒
没有乱码
#4
你那转换的地方根本没用。reault这个字符串里已经是乱码了,你转那么俩次跟没转一样,在流那块弄下试试
就是不会弄啊,大神指教指教啊
String result = EntityUtils.toString(httpEntity, "utf-8"); 不用getContent那些步骤就可以获取,至于流里那块来转码还不是很清楚
#5
你那转换的地方根本没用。reault这个字符串里已经是乱码了,你转那么俩次跟没转一样,在流那块弄下试试
就是不会弄啊,大神指教指教啊
我日,弄错了,你这根本就不是utf-8,而是GBK! 你把这块加上编码就可以了BufferedReader reader = new BufferedReader(new InputStreamReader(is, "gbk"));
#6
客户端转换时使用的字符编码要和服务器端的字符编码一致,一般选UTF-8
#7
自定httpclient的时候可以设置编码规则
#8
是UTF8编码么?