HttpURLConnection 下载图片

HttpURLConnection 下载图片,第1张

概述1.activity_main.xml<?xmlversion="1.0"encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apkes/android"xmlns:app="http://schemas.android.com/

1.activity_main.xml

<?xml version="1.0" enCoding="utf-8"?><androIDx.constraintlayout.Widget.ConstraintLayout xmlns:androID="http://schemas.androID.com/apk/res/androID"    xmlns:app="http://schemas.androID.com/apk/res-auto"    xmlns:tools="http://schemas.androID.com/tools"    androID:layout_wIDth="match_parent"    androID:layout_height="match_parent"    tools:context=".MainActivity">    <button        androID:ID="@+ID/button"        androID:layout_wIDth="wrap_content"        androID:layout_height="wrap_content"        androID:layout_margintop="28dp"        androID:layout_marginEnd="16dp"        androID:text="查看"        androID:onClick="onClickbutton"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constrainttop_totopOf="parent" />    <EditText        androID:ID="@+ID/editTextTextPersonname"        androID:layout_wIDth="0dp"        androID:layout_height="wrap_content"        androID:ems="10"        androID:inputType="textPersonname"        androID:text="https://www.baIDu.com"        app:layout_constraintEnd_toStartOf="@+ID/button"        app:layout_constraintHorizontal_bias="0.0"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_totopOf="@+ID/button" />    <ScrollVIEw        androID:layout_wIDth="0dp"        androID:layout_height="0dp"        app:layout_constraintBottom_toBottomOf="parent"        app:layout_constraintEnd_toEndOf="parent"        app:layout_constraintStart_toStartOf="parent"        app:layout_constrainttop_toBottomOf="@+ID/editTextTextPersonname">        <linearLayout            androID:layout_wIDth="match_parent"            androID:layout_height="wrap_content"            androID:orIEntation="vertical">            <TextVIEw                androID:ID="@+ID/textVIEw2"                androID:layout_wIDth="match_parent"                androID:layout_height="match_parent"                androID:text="显示查看的网页内容" />        </linearLayout>    </ScrollVIEw></androIDx.constraintlayout.Widget.ConstraintLayout>

2.MainActivity

package com.daliang.demo_http1;import androIDx.annotation.NonNull;import androIDx.appcompat.app.AppCompatActivity;import androID.annotation.Suppresslint;import androID.os.Bundle;import androID.os.DeBUG;import androID.os.Handler;import androID.os.Message;import androID.vIEw.VIEw;import androID.Widget.EditText;import androID.Widget.TextVIEw;import androID.Widget.Toast;import java.io.BufferedReader;import java.io.IOException;import java.io.inputStream;import java.io.inputStreamReader;import java.net.httpURLConnection;import java.net.ProtocolException;import java.net.URL;import java.util.logging.LogRecord;public class MainActivity extends AppCompatActivity {    final int REQ_SUCCESS=1;    final  int REQ_ERROR=2;    private  String path;    private EditText inputText;    private TextVIEw textVIEw;    @Suppresslint("HandlerLeak")     Handler handler =new Handler(){         @OverrIDe         public voID handleMessage(@NonNull Message msg) {            switch (msg.what) {                case REQ_SUCCESS:                    String text = (String) msg.obj;                    textVIEw.setText(text);                    break;                case REQ_ERROR:                    Toast.makeText(MainActivity.this,"连接网络失败!",Toast.LENGTH_LONG).show();                    break;            }         }     };    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_main);        inputText=findVIEwByID(R.ID.editTextTextPersonname);        textVIEw=findVIEwByID(R.ID.textVIEw2);    }    public voID onClickbutton(VIEw vIEw) {        path=inputText.getText().toString();        textVIEw.setText(null);        new Thread() {            @OverrIDe            public voID run() {                hellohttp();            }        }.start();    }    public voID hellohttp()  {        BufferedReader reader=null;        httpURLConnection conn=null;        try {            URL url =new URL(path);            conn =(httpURLConnection) url.openConnection();            conn.setRequestMethod("GET");            conn.setConnectTimeout(5000);            conn.connect();//发起连接            int code=conn.getResponseCode();//200表示连接成功            System.out.println("Code:"+code);            if (code==200)            {                inputStream inputStream=conn.getinputStream();                reader=new BufferedReader(new inputStreamReader(inputStream));                StringBuilder result=new StringBuilder();                String line;                while ((line=reader.readline())!=null)                {                    result.append(line);                }                System.out.println("连接网络成功!");                Message msg=new Message();                msg.what=REQ_SUCCESS;                msg.obj=result.toString();                handler.sendMessage(msg);            }            else            {                Message msg=new Message();                msg.what=REQ_ERROR;                msg.obj="连接网络失败!";                handler.sendMessage(msg);                System.out.println("连接网络失败!");            }        } catch (Exception e) {            System.out.println("Exception:"+e);            Message msg=new Message();            msg.what=REQ_ERROR;            msg.obj="连接网络失败!";            handler.sendMessage(msg);            System.out.println("连接网络失败!");            e.printstacktrace();        }        finally {            if (conn != null) conn.disconnect();            if (reader != null) {                try {                    reader.close();                } catch (IOException e) {                    e.printstacktrace();                }            }        }    }}

 

总结

以上是内存溢出为你收集整理的HttpURLConnection 下载图片全部内容,希望文章能够帮你解决HttpURLConnection 下载图片所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1004338.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-22
下一篇 2022-05-22

发表评论

登录后才能评论

评论列表(0条)

保存