1.要在andorid中实现网络图片查看,涉及到用户隐私问题,所以要在AndroidManifest.xml中添加访问网络权限

<uses-permission android:name="android.permission.INTERNET"/>

2.布局文件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:orientation="vertical" >

<EditText

android:id="@+id/path"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="http://10.162.0.171:8080/html/index.jsp"

/>

<Button

android:id="@+id/button"

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="查看"

android:onClick="onClick"

/>

<ScrollView

android:layout_width="fill_parent"

android:layout_height="fill_parent"

>

<TextView

android:id="@+id/text"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

/>

</ScrollView>

</LinearLayout>

3.MainActivity.java

package com.example.html;

import java.io.InputStream;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import org.apache.http.HttpConnection;

import android.os.Bundle;

import android.os.Handler;

import android.os.Message;

import android.app.Activity;

import android.text.TextUtils;

import android.util.Log;

import android.view.Menu;

import android.view.View;

import android.widget.EditText;

import android.widget.TextView;

import android.widget.Toast;

public class MainActivity extends Activity {

private EditText path;

private TextView text;

private final int SUCCESS=1;

private final int ERROR=2;

/**

* 主线程创建消息处理器

*/

private Handler handler = new Handler(){

@Override

public void handleMessage(Message msg) {

if(msg.what==SUCCESS){

String showhtml =(String) msg.obj;

text.setText(showhtml);

}else if(msg.what==ERROR){

Toast.makeText(MainActivity.this, "html加载失败", Toast.LENGTH_LONG).show();

}

}

};

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

path = (EditText) findViewById(R.id.path);

text = (TextView) findViewById(R.id.text);

}

public void onClick(View view) {

final String textPath = path.getText().toString();

if(!TextUtils.isEmpty(textPath)){

new Thread(){

@Override

public void run() {

try {

URL url = new URL(textPath);

int code = conn.getResponseCode();

if(code==200){

InputStream is = conn.getInputStream();

String s=StreamService.getInputStream(is);

Message msg = new Message();

msg.what=SUCCESS;

msg.obj=s;

handler.sendMessage(msg);

}else{

System.out.println("code"+code);

InputStream is = conn.getInputStream();

String s=StreamService.getInputStream(is);

System.out.print(s);

Message msg = new Message();

msg.what=ERROR;

handler.sendMessage(msg);

}

} catch (Exception e) {

// TODO Auto-generated catch block

System.out.println("textPath"+textPath);

e.printStackTrace();

Message msg = new Message();

msg.what=ERROR;

handler.sendMessage(msg);

}

}

}.start();

}else{

Toast.makeText(MainActivity.this, "访问路径不能为空", Toast.LENGTH_LONG).show();

}

}

}

4.StreamService.java

package com.example.html;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

public class StreamService {

public static String getInputStream(InputStream iSteam) throws IOException{

ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len=0;

if((len=iSteam.read(buffer))!=-1){ //把InputStream读入到内存当做

baos.write(buffer, 0, len); //写入内存

}

iSteam.close();

baos.close();

byte[] result=baos.toByteArray();

String html = new String(result, "utf-8");

return html;

}

}

以上毫无疑问地成功,细心地我们发现,如果把index.jsp的编码改为gbk,gb2312等等的字符编码,上面的代码会显示成功的界面吗?

经过验证,把index.jsp的编码改为gbk,gb2312。结果是:

Android界面中出现了乱码。

原因在于:

public class StreamService {

public static String getInputStream(InputStream iSteam) throws IOException{

ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len=0;

if((len=iSteam.read(buffer))!=-1){ //把InputStream读入到内存当做

baos.write(buffer, 0, len); //写入内存

}

iSteam.close();

baos.close();

byte[] result=baos.toByteArray();

String html = new String(result, "utf-8");

return html;

}

我们得到字节result后,转化为字符串的字符是utf-8。

String temp = new String(result);在这方法中android系统默认的字符为utf-8

如何index.jsp中的编码是gbk,gb2312,并且返回的字符串也没有设定编码。这样就会与android默认返回的编码utf-8冲突,所以出现乱码

解决字符串编码问题

修改StreamService.java

package com.example.html;

import java.io.ByteArrayOutputStream;

import java.io.IOException;

import java.io.InputStream;

public class StreamService {

public static String getInputStream(InputStream iSteam) throws IOException{

ByteArrayOutputStream baos = new ByteArrayOutputStream();

byte[] buffer = new byte[1024];

int len=0;

if((len=iSteam.read(buffer))!=-1){ //把InputStream读入到内存当做

baos.write(buffer, 0, len); //写入内存

}

iSteam.close();

baos.close();

byte[] result=baos.toByteArray();

//解决中文乱码

//解析result里面的字符串

String temp = new String(result);

if(temp.contains("utf-8")){ //编码为utf-8时,直接返回字符串

return temp;

}else if(temp.contains("gbk")){  //如果不是utf-8,则设置返回的编码

String html = new String(result, "gbk");

return html;

}else if(temp.contains("gb3212")){

String html = new String(result, "gb3212");

return html;

}

return temp;

}

}

修改后结果:

http网络通信--页面源代码查看的更多相关文章

  1. PHP 在源代码中实现换行使得页面源代码更精致美观

    常量 : PHP_EOL 换行实例: <? php echo $this->doctype($this->doctype) . PHP_EOL;?> <html> ...

  2. 117 FP页面无法查看

    用户表示117 FP页面无法查看,提示如下错误: 跟进: 1.进入FP服务器可看到以下错误 这个错误的框就表示FP的一个进程报错,自动断掉了,需要重新跑一次EXIT,INIT,PLAN,EXPORT, ...

  3. Spring Security默认的用户登录表单 页面源代码

    Spring Security默认的用户登录表单 页面源代码 <html><head><title>Login Page</title></hea ...

  4. HTML 页面源代码布局介绍

    http://www.cnblogs.com/polk6/archive/2013/05/10/3071451.html 此介绍以google首页源代码截图为例: 从上到下依次介绍: 1.<!D ...

  5. 117 FP页面无法查看 此错误是JDK8.0.0.0版本的一个BUG,会导致工单重复回写,

    用户表示117 FP页面无法查看,提示如下错误: 跟进: 1.进入FP服务器可看到以下错误 这个错误的框就表示FP的一个进程报错,自动断掉了,需要重新跑一次EXIT,INIT,PLAN,EXPORT, ...

  6. Struts2源代码查看

    -----------------siwuxie095 Struts2 源代码查看 1.Struts2 的核心过滤器 StrutsPrepareAndExecuteFilter 实现了 Filter ...

  7. 调整 firefox 源代码查看器的字体

    默认的 firefox 源代码查看器的字体很不好看,不适合阅读代码.想要修改,又没有发现入口.如何修改呢?实际上在选项里的字体就能改.方法如下: 打开选项页,在字体一栏点击高级,把等宽字体由“宋体”改 ...

  8. HDFS原理解析(整体架构,读写操作流程及源代码查看等)

    前言 HDFS 是一个能够面向大规模数据使用的.可进行扩展的文件存储与传递系统.是一种同意文件通过网络在多台主机上分享的文件系统,可让多机器上的多用户分享文件和存储空间.让实际上是通过网络来訪问文件的 ...

  9. Selenium入门16 获取页面源代码

    页面源代码:page_source属性 获取源代码之后,再用正则表达式匹配出所有的链接,代码如下: #coding:utf-8 from selenium import webdriver impor ...

随机推荐

  1. 安卓屏幕旋转时,禁止Activity重新加载

    安卓设备旋转屏幕时,Activity默认会重新加载,如果是要读取大量数据的场景,那等待的时间比较长,这一点不可接受,所以要想办法禁止Activity自动重新加载. 方法如下在AndroidManife ...

  2. Ionic-wechat项目边开发边学(四):可伸缩输入框,下拉刷新, 置顶删除

    摘要 上一篇文章主要介绍了ion-list的使用, ion-popup的使用, 通过sass自定义样式, localStorage的使用, 自定义指令和服务. 这篇文章实现的功能有消息的置顶与删除, ...

  3. BootstrapTable与KnockoutJS相结合实现增删改查功能

    http://www.jb51.net/article/83910.htm KnockoutJS是一个JavaScript实现的MVVM框架.通过本文给大家介绍BootstrapTable与Knock ...

  4. 运行时候报异常could only be replicated to 0 nodes instead of minReplication (=1). There are 2 datanode(s) running and no node(s) are excluded in this operation.

    运行时候报异常could only be replicated to 0 nodes instead of minReplication (=1).  There are 2 datanode(s) ...

  5. python之IO model

    一.事件驱动模型 在介绍协程时,遇到IO操作就切换,但什么时候切换回来,怎么确定IO操作结束? 很多人可能会考虑使用“线程池”或“连接池”.“线程池”旨在减少创建和销毁线程的频率,其维持一定合理数量的 ...

  6. logstash的grok正则匹配规则文件

    文件路径:logstash/vendor/bundle/jruby/1.9/gems/logstash-patterns-core-4.1.0/patterns/grok-patterns 在线调试g ...

  7. Html导出Pdf

    最近领导给了一个新的需求:给了我一个html页面,让我导出Pdf页面 由于以前没有搞过这方面的需求,所以查百度找资料,找了一大堆的好东西,像ItextSharp和wkhtmltopdf 废话不说,进入 ...

  8. Linux基础系列-Day6

    Samba服务(基于CentOS 7.0) Samba是在Linux和UNIX系统上实现SMB协议的一个免费软件,由服务器及客户端程序构成,Samba主要用于Linux或UNIX和Windows系统之 ...

  9. ARP扫描工具arp-scan

    ARP扫描工具arp-scan   arp-scan是Kali Linux自带的一款ARP扫描工具.该工具可以进行单一目标扫描,也可以进行批量扫描.批量扫描的时候,用户可以通过CIDR.地址范围或者列 ...

  10. 输入输出格式之Python版

    # 有多组输入数据,但没有具体的告诉你有多少组,只是让你对应每组输入,应该怎样输出. while True: try: a, b = map(int, raw_input().strip().spli ...