当前位置 : 主页 > 编程语言 > java >

图片查看器以及网页源码查看

来源:互联网 收集:自由互联 发布时间:2021-06-28
StreamTools.java package com.example.watchjpgdemo;import java.io.ByteArrayOutputStream;import java.io.InputStream;/** * Created by Lancer on 2017/11/9. */public class StreamTools { public static String readStream(InputStream is) throws Exce
StreamTools.java
package com.example.watchjpgdemo;

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

/**
 * Created by Lancer on 2017/11/9.
 */

public class StreamTools {
    public static String readStream(InputStream is) throws Exception {
        ByteArrayOutputStream baos=new ByteArrayOutputStream();
        byte[] buffer=new byte[1024];
        int len=-1;
        while ((len=is.read(buffer))!=-1){
            baos.write(buffer,0,len);
        }
        is.close();
        String temp=baos.toString("gb2312");
        return  temp;
    }
}
HttpUtil.java
package com.example.watchjpgdemo;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;


public class HttpUtil {
    public static String sendHttpRequest(String address){
        HttpURLConnection coon=null;
        try {
            URL url=new URL(address);
            coon= (HttpURLConnection) url.openConnection();
            coon.setRequestMethod("GET");
            coon.getConnectTimeout();
            coon.getReadTimeout();
            coon.setDoInput(true);
            coon.setDoOutput(true);
            InputStream is=coon.getInputStream();
            BufferedReader reader=new BufferedReader(new InputStreamReader(is));
            StringBuilder response=new StringBuilder();
            String line;
            while ((line=reader.readLine())!=null){
                response.append(line);
            }
            return response.toString();
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }finally {
        if (coon!=null){
            coon.disconnect();
            }
        }
    }
}
MainActivity.java
package com.example.watchjpgdemo;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {
    private ImageView iv;
    private EditText et;
    private View view;
    private Handler handler = new Handler() {

        @Override
        public void handleMessage(Message msg) {
            Bitmap bitmap = (Bitmap) msg.obj;
            iv.setImageBitmap(bitmap);
            super.handleMessage(msg);
        }
    };


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        et = (EditText) findViewById(R.id.et);
        iv = (ImageView) findViewById(R.id.iv);
    }

    public void click(View view) {
        final String path = et.getText().toString().trim();
        new Thread() {
            @Override
            public void run() {
                try {
                    URL url = new URL(path);
                    HttpURLConnection coon = (HttpURLConnection) url.openConnection();
                    coon.setRequestMethod("GET");
                    int contentLength = coon.getContentLength();
                    Log.d("Main", "返回长度是" + contentLength);
                    int code = coon.getResponseCode();
                    if (code == 200) {
                        InputStream stream = coon.getInputStream();
                        Bitmap bitmap = BitmapFactory.decodeStream(stream);
                        Message msg = new Message();
                        msg.obj = bitmap;
                        handler.sendMessage(msg);
                        stream.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }.start();

    }

}
HttpActivity.java
package com.example.watchjpgdemo;

import android.app.ProgressDialog;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class HttpActivity extends AppCompatActivity {
    private EditText et;
    private TextView tv;
    private String str;
    private ProgressDialog pd;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            pd.dismiss();
            String datas= (String) msg.obj;
            tv.setText(datas);
            super.handleMessage(msg);
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_http);
        et= (EditText) findViewById(R.id.et);
        tv= (TextView) findViewById(R.id.tv);
    }
    public void look(View view){
        final String path=et.getText().toString().trim();
        pd = new ProgressDialog(HttpActivity.this);
        pd.setMessage("正在获取数据");
        pd.show();
        new Thread(){
            @Override
            public void run() {
                try {
                    URL url=new URL(path);
                    HttpURLConnection coon = (HttpURLConnection) url.openConnection();
                    coon.setRequestMethod("GET");
                    int code = coon.getResponseCode();
                    int length = coon.getContentLength();
                    System.out.println(length);
                    System.out.println(code);
                    if (code==200){
                        InputStream in = coon.getInputStream();
                        /*byte[] buffer=new byte[1024];
                        int len=-1;

                        while((len=in.read())!=-1){
                            str=new String(buffer,0,len);
                        }*/
                        String data = StreamTools.readStream(in);
                        Message message = new Message();
                        message.obj=data;
                        handler.sendMessage(message);
                in.close();
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }

            }
        }.start();
    }
}
activity_main.xml
 

 

    
  
    
  

    
  

 
activity_http.xml
网友评论