0

I want to get web page source(which is written by me in php) then it shows in textview. However, it returns always null.

I use permisson(INTERNET) but It doesn't work.

When I run this app, TextView shows: "Kaynak Kod: null"

Here is my Activity Codes:

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

}

Manifest:

<?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>

Regards.

Ömer ASLAN
  • 139
  • 3
  • 15

3 Answers3

1

You have a NetworkOnMainThreadException Look in the LogCat to see it. You have to place your code in an AsyncTask or thread.

greenapps
  • 11,154
  • 2
  • 16
  • 19
  • How can I place this code ? Could you give an example ? Thx. – Ömer ASLAN Aug 08 '14 at 18:46
  • There are so many examples on this site. Google for 'upload with asynctask' or dowmload. Does not make a difference. – greenapps Aug 08 '14 at 18:50
  • Thank you but is it relevant to see web page source code ? – Ömer ASLAN Aug 08 '14 at 18:51
  • It is relevant for downloading data from internet. What you then do with the downloaded data is irrelevant. – greenapps Aug 08 '14 at 18:53
  • I see lots of application for web service for android. There is no any application which use asynctask or thread. However, I cannot reach source code.. – Ömer ASLAN Aug 08 '14 at 19:01
  • Nonsense. If you google for ´download with asynctask´ then the third link is http://stackoverflow.com/questions/3090650/android-loading-an-image-from-the-web-with-asynctask – greenapps Aug 08 '14 at 19:04
0

You can give something like this a try:

HttpGet request = new HttpPost(url);
HttpResponse response = httpclient.execute(request);
String responseBody = EntityUtils.toString(response.getEntity()); 
Mr. Concolato
  • 2,224
  • 5
  • 24
  • 44
0

All you have to do is include this method in your code :

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;
}

pass in the URL as parameter and it returns the code

Rahul Ravindran
  • 304
  • 6
  • 16