2

我试图从 url 读取 HTML 内容。我尝试了来自许多站点的许多示例和代码示例,但它对我不起作用。当我运行代码时,它会在 textview 中保留默认文本。我什至也尝试过使用edittext。我什至在清单文件中添加了权限。

我使用的代码之一在这里:我在这里添加了完整的代码。我尝试使用下面的所有代码都没有成功。

 package com.adn;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Activity;
import android.os.Bundle;
import android.widget.EditText;

public class REEActivity extends Activity {
    private static BufferedReader reader = null;

    /** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    EditText ed = (EditText) findViewById(R.id.editText1);

    try {
        ed.append(getStringFromUrl("http://www.google.com"));
        //getInputStreamFromUrl("").close();
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public static InputStream getInputStreamFromUrl(String url){
       InputStream contentStream = null;

       try{
         HttpClient httpclient = new DefaultHttpClient();
         HttpResponse response = httpclient.execute(new HttpGet(url));
         contentStream = response.getEntity().getContent();
       } catch(Exception e){
          e.printStackTrace();
       }
       System.out.println("Content stream is " + contentStream);
       return contentStream;
    }


public static String getStringFromUrl(String url) throws IOException{
    reader = new BufferedReader(new InputStreamReader(getInputStreamFromUrl(url)));

    StringBuilder sb = new StringBuilder();
    try{
    String line = null;
    while((line = reader.readLine()) != null)
    {
        sb.append(line);
    }    
    }catch (IOException e){
     e.printStackTrace();
    }
   getInputStreamFromUrl(url).close();
    return sb.toString();
}
}

这是日志猫:

    06-20 21:25:18.022: E/AndroidRuntime(14095): FATAL EXCEPTION: main
06-20 21:25:18.022: E/AndroidRuntime(14095): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adn/com.adn.REEActivity}: java.lang.NullPointerException
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread.access$600(ActivityThread.java:123)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.os.Looper.loop(Looper.java:137)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread.main(ActivityThread.java:4424)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at java.lang.reflect.Method.invokeNative(Native Method)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at java.lang.reflect.Method.invoke(Method.java:511)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at dalvik.system.NativeStart.main(Native Method)
06-20 21:25:18.022: E/AndroidRuntime(14095): Caused by: java.lang.NullPointerException
06-20 21:25:18.022: E/AndroidRuntime(14095):    at java.io.Reader.<init>(Reader.java:64)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at java.io.InputStreamReader.<init>(InputStreamReader.java:79)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at com.adn.REEActivity.getStringFromUrl(REEActivity.java:49)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at com.adn.REEActivity.onCreate(REEActivity.java:28)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.Activity.performCreate(Activity.java:4465)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
06-20 21:25:18.022: E/AndroidRuntime(14095):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
06-20 21:25:18.022: E/AndroidRuntime(14095):    ... 11 more

这是清单文件

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

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

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".REEActivity"
            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>

有人可以帮我解决这个问题。我也是安卓新手。

4

2 回答 2

1

代替

StringBuffer sb = new StringBuffer();

StringBuffer sb = new StringBuilder();

并使用 close() 方法关闭您的 InputStrem

尝试在第二种方法中使用此代码,希望这能解决问题

public static String getStringFromUrl(String url) throws UnsupportedEncodingException{
    BufferedReader br = new BufferedReader(new InputStreamReader(getInputStreamFromUrl(url)));

    StringBuilder sb = new StringBuilder();
    try{
    String line = null;
    while((line = reader.readLine()) != null)
    {
        sb.append(line);
    }    
    }catch (IOException e){
     e.printStackTrace();
    }
    getInputStreamFromUrl(url).close();
    return sb.toString();
}

我尝试了您更新的代码,它现在可以工作了,这就是我得到的:

在此处输入图像描述

于 2012-06-20T16:28:35.373 回答
0

你能试试这个代码吗

public String getUrlContents(String url){
    String content = "";
    HttpClient hc = new DefaultHttpClient();
    HttpGet hGet = new HttpGet(url);
    ResponseHandler<String> rHand = new BasicResponseHandler();
    try {
        content = hc.execute(hGet,rHand);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return content;
}
于 2012-06-20T16:28:16.873 回答