如何从url(android、java)获取网站的html源代码?

如何从url(android、java)获取网站的html源代码?,java,android,networking,Java,Android,Networking,我正在制作一个简化的应用程序,其中只有一个EditText,用户输入URL地址和一个按钮“下载”。当用户点击按钮时,应用程序应该下载给定网站的源代码并将其打印在屏幕上。我怎么做?或者我应该从哪里开始寻找解决方案 首先在您的清单文件中为INTERNET as授予权限 <uses-permission android:name="android.permission.INTERNET" /> 现在,如果你在编辑文本中给出url 它将显示图像或者我应该在哪里寻找解决方案?< /代码>在I

我正在制作一个简化的应用程序,其中只有一个EditText,用户输入URL地址和一个按钮“下载”。当用户点击按钮时,应用程序应该下载给定网站的源代码并将其打印在屏幕上。我怎么做?或者我应该从哪里开始寻找解决方案

首先在您的清单文件中为INTERNET as授予权限

<uses-permission android:name="android.permission.INTERNET" />
现在,如果你在编辑文本中给出url
它将显示图像

<代码>或者我应该在哪里寻找解决方案?< /代码>在Internet上搜索对URL进行GET请求,并将结果显示为文本。您考虑使用WebVIEW吗?或者您想显示URL的源代码?可能这将帮助您@Timo我想显示源代码我需要显示输入URL的源代码而不是图像,但我会尝试调整代码以适应我的任务。谢谢尼古拉,你的问题解决了吗?可能是重复的@Nikola,你似乎已经改变了你的问题,首先你要求使用URL下载图片,所以我从零开始发布了上述代码。我没有,请再次阅读我的问题。我知道myquestion是的副本,但给定链接中的所有解决方案现在都不推荐使用。欢迎@Nikola
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_url"
            android:layout_gravity="center_horizontal" />
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_gravity="bottom">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_weight="1">

            <Button
                android:layout_width="233dp"
                android:layout_height="58dp"
                android:text="New Button"
                android:id="@+id/downloadImage"
                android:layout_gravity="center"
                android:background="@color/blue"
                android:clickable="true" />
        </LinearLayout>

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="145dp"
            android:layout_height="58dp">

            <ImageView
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/imageView786" />
        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="45dp">

    </LinearLayout>
</LinearLayout>
public class Download extends Activity {
Button bt;
ImageView iv;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.download);
    bt = (Button)findViewById(R.id.downloadImage);
    et = (EditText )findViewById(R.id.et_url);
    iv = (ImageView)findViewById(R.id.imageView786);
    bt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            DownloadImageFromPath(et.getText().toString());
        }
    });

}
public void DownloadImageFromPath(String path){
    InputStream in =null;
    Bitmap bmp=null;
    //ImageView iv = (ImageView)findViewById(R.id.imageView786);
    int responseCode = -1;
    try{

        URL url = new URL(path);//"http://192.xx.xx.xx/mypath/img1.jpg
        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setDoInput(true);
        con.connect();
        responseCode = con.getResponseCode();
        if(responseCode == HttpURLConnection.HTTP_OK)
        {
            //download
            in = con.getInputStream();
            bmp = BitmapFactory.decodeStream(in);
            in.close();
            iv.setImageBitmap(bmp);
        }

    }
    catch(Exception ex){
        Log.e("Exception", ex.toString());
    }
}