【实战】android网页源代码查看器
16lz
2021-01-23
点击查看源代码,可以查看该网页的源代码
一、文件结构
MainActivity.java:
package com.example.viewpagesource;import java.io.InputStream;import java.net.HttpURLConnection;import java.net.MalformedURLException;import java.net.URL;import java.net.URLConnection;import com.example.viewpagesource.utils.StreamTool;import android.os.Bundle;import android.os.Handler;import android.os.Message;import android.app.Activity;import android.text.TextUtils;import android.view.Menu;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity implements View.OnClickListener {protected static final int SUCCESS = 0;protected static final int ERROR = 1;protected static final int NETWORK_ERROR = 2;private Button btn;private EditText ed_path;private TextView tv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn=(Button) findViewById(R.id.btn); btn.setOnClickListener(this); ed_path=(EditText) findViewById(R.id.ed_path); tv=(TextView) findViewById(R.id.tv); } //定义一个秘书 Handler handler=new Handler(){ public void handleMessage(android.os.Message msg) { switch (msg.what) {case SUCCESS:String value=(String) msg.obj;tv.setText(value);break;case ERROR:System.out.println("ERROR");Toast.makeText(MainActivity.this, "错误发生了", 0).show();break;case NETWORK_ERROR:System.out.println("NETWORK_ERROR");Toast.makeText(MainActivity.this, "错误发生了", 0).show();break;default:break;} }; }; String path;@Overridepublic void onClick(View v) {// TODO Auto-generated method stubpath = ed_path.getText().toString().trim();if(TextUtils.isEmpty(path)){Toast.makeText(this, "路径有错误", 0).show();return;}//连接网络,要启动一个新的线程去,干耗时的事情new Thread(){public void run() {try {URL url=new URL(path);HttpURLConnection conn = (HttpURLConnection) url.openConnection();//设置连接超时为5秒钟conn.setConnectTimeout(5000);conn.setRequestMethod("GET");String contentType = conn.getContentType();int code = conn.getResponseCode();if(code==200){InputStream in = conn.getInputStream();//这里的data是从服务器返回的String data=StreamTool.decodeStream(in);//展示在屏幕上Message msg=Message.obtain();msg.what=SUCCESS;msg.obj=data;handler.sendMessage(msg);}else{Message msg=Message.obtain();msg.what=ERROR;handler.sendMessage(msg);}} catch (Exception e) {// TODO Auto-generated catch blockMessage msg=Message.obtain();msg.what=NETWORK_ERROR;handler.sendMessage(msg);e.printStackTrace();}};}.start();} }
StreamTool.java
package com.example.viewpagesource.utils;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.io.InputStream;public class StreamTool {public static String decodeStream(InputStream in) throws IOException {// TODO Auto-generated method stub//底层流ByteArrayOutputStream baos=new ByteArrayOutputStream();int len=0;byte[] buf=new byte[1024];while((len=in.read(buf))>0){baos.write(buf,0,len);}String data = baos.toString();return data;}}
activity_main.xml:
AndroidMainfest.xml
更多相关文章
- Android App安装失败错误总结
- Android源代码查看途径
- [Android Studio导入第三方类库方法] Error:(19, 23) 错误: 程序
- Android 布局加载源代码分析
- Android Permission denied 错误(附Android权限大全)
- JS获取整个HTML网页代码 - Android 集美软件园 - 博客频道 - CSD
- Android系统源代码下载
- Android Permission denied 错误 ( 附Android权限大全 )
- android install faild insufficient storage错误的解决方法