android - 如何获取Web源代码? (null例外)

时间:2014-08-08 18:21:23

标签: java android httpwebrequest httpwebresponse

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

我使用许可(互联网),但它不起作用。

当我运行此应用程序时,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>

问候。

3 个答案:

答案 0 :(得分:1)

NetworkOnMainThreadException中有一个LogCat外观可供查看。您必须将代码放在AsyncTaskthread

答案 1 :(得分:0)

你可以尝试这样的东西:

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

答案 2 :(得分:0)

您只需在代码中包含此方法:

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作为参数,它返回代码