【问题标题】:android - how to get web source code ? (null exception)android - 如何获取网页源代码? (空异常)
【发布时间】:2014-08-08 18:21:23
【问题描述】:

我想获取网页源代码(由我用 php 编写)然后它显示在 textview 中。但是,它总是返回 null。

我使用 permisson(INTERNET) 但它不起作用。

当我运行这个应用程序时,TextView 显示:“Kaynak Kod: null”

这是我的活动代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    tv = (TextView)findViewById(R.id.textView1);
    Button button = (Button)findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener(){

        @Override
        public void onClick(View v) {
            try {
                String source = getData("http://www.oeaslan.com/excel/index_.php?gun=1");
                tv.setText("Kaynak Kod: "+source);
            } catch (Exception e) {
                tv.setText("Hata: "+e.getMessage());
            }
        }

    });
}

private String getData(String url){
    try{
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(url);
        HttpResponse response = client.execute(request);

        String html = "";
        InputStream in = response.getEntity().getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(in));
        StringBuilder str = new StringBuilder();
        String line = null;
        while((line = reader.readLine()) != null)
        {
            str.append(line);
        }
        in.close();
        html = str.toString();
        return html;
    }catch(Exception e){
        return e.getMessage();
    }

}

清单:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.omer.text"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />
<uses-permission android:name="android.permission.INTERNET" />
<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

问候。

【问题讨论】:

    标签: java android httpwebrequest httpwebresponse


    【解决方案1】:

    你有一个NetworkOnMainThreadExceptionLogCat 中查看它。您必须将代码放在AsyncTaskthread 中。

    【讨论】:

    • 如何放置此代码?你能举个例子吗?谢谢。
    • 这个网站上有很多例子。谷歌“使用异步任务上传”或下载。没有区别。
    • 谢谢,但是看网页源代码有关系吗?
    • 与从互联网下载数据有关。然后你对下载的数据做什么是无关紧要的。
    • 我看到很多用于 android 的 Web 服务应用程序。没有任何使用异步任务或线程的应用程序。但是,我无法访问源代码..
    【解决方案2】:

    你可以试试这样的:

    HttpGet request = new HttpPost(url);
    HttpResponse response = httpclient.execute(request);
    String responseBody = EntityUtils.toString(response.getEntity()); 
    

    【讨论】:

      【解决方案3】:

      你所要做的就是在你的代码中包含这个方法:

      public static String getSourceCodeOfWebsite(String urlget){
          String fetched_data ="";
          try {
              URL url = null;
              url = new URL(urlget);
              HttpURLConnection connection = (HttpURLConnection) url.openConnection();
              connection.connect();
              StringBuilder response = new StringBuilder(50000);
              InputStream inputStream = connection.getInputStream();
              BufferedReader rd = new BufferedReader(new InputStreamReader(inputStream));
              int i = 0;
              while ((i = rd.read()) > 0) {
                  response.append((char)i);
              }
              fetched_data = response.toString();
          } catch (Exception e) {
              e.printStackTrace();
          }
      
          return fetched_data;
      }
      

      将 URL 作为参数传入并返回代码

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2010-12-21
        • 2021-02-17
        • 1970-01-01
        相关资源
        最近更新 更多