【问题标题】:How to get the html-source of a website from a url (android, java)?如何从 url (android, java) 获取网站的 html 源代码?
【发布时间】:2017-05-24 11:50:52
【问题描述】:

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

【问题讨论】:

  • Or where I should start looking for a solution?上网搜索
  • 向 URL 发出 GET 请求并将结果显示为文本。
  • 你考虑过使用 WebView 吗?还是要显示网址的源代码?
  • 也许这会对你有所帮助stackoverflow.com/questions/2423498/…
  • @Timo 我要显示源代码

标签: java android networking


【解决方案1】:

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

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

这是你的布局

    <?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());
    }
}

现在,如果您将在编辑文本中提供 url http://i.imgur.com/bIRGzVO.jpg会显示图片

【讨论】:

  • 我需要显示输入 URL 的源代码而不是图像,但我会尝试使代码适应我的任务。谢谢!
  • 你解决了你的问题@Nikola 吗?可能是stackoverflow.com/questions/2423498/…的重复项
  • @Nikola 看来你已经改变了你的问题,首先你要求使用 URL 下载图像,所以我从头开始发布上面的代码..
  • 我没有,请再次阅读我的问题。我知道 myquestion 是 stackoverflow.com/questions/2423498/… 的副本,但现在不推荐使用给定链接中的所有解决方案。
  • 欢迎@Nikola
猜你喜欢
  • 1970-01-01
  • 2017-01-26
  • 2019-01-14
  • 2013-12-09
  • 1970-01-01
  • 2011-07-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多