0

I'm trying to connect any web page by the easiest way. I learnt that i have to use AsyncTask but i want before to see this job.

package com.example.duzbaglanti;

import java.io.BufferedInputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity {



    String biyazi = "ggg";
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);

        tv = (TextView) findViewById(R.id.textView1);
        Button benimtus = (Button) findViewById(R.id.button1);

        benimtus.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                tv.setText(biyazi);
            }
        });



        HttpURLConnection benimconnection = null;
        try {
            URL benimurl = new URL("anypage");
            benimconnection = (HttpURLConnection) benimurl.openConnection();
            int sonuckodu = benimconnection.getResponseCode();
            if(sonuckodu == HttpURLConnection.HTTP_OK) {
                InputStream benimin = new BufferedInputStream(benimconnection.getInputStream());
                biyazi = benimin.toString();

                ///////
            }
        }
        catch (Exception e) {
            Log.d("baglantihatasi","hata oldu",e);
        }
        finally { benimconnection.disconnect(); }




    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

when i click the button my string changes to 'Java.io.BufferedInputStream@4177b2a8' not my web page's output. what is the wrong?

fuatkaraca
  • 47
  • 8

2 Answers2

0

Have you looked at the implementation of BufferedInputStream#toString()?

InputStream benimin = new BufferedInputStream(benimconnection.getInputStream());
biyazi = benimin.toString();

There isn't actually one. So the method call defers to Object#toString() as that is the first available implemented parent class method. That's implemented as such

public String toString() {
    return getClass().getName() + "@" + Integer.toHexString(hashCode());
}

In other words it has nothing to do with the content of the InputStream. You need to actually read the stream into, possibly, an array of bytes and then convert that into a String. Here's one way.

Community
  • 1
  • 1
Sotirios Delimanolis
  • 274,122
  • 60
  • 696
  • 724
0

Check your manifest file, whether you have added internet permission to your manifest file, If not, add it. Then it works!

nki
  • 192
  • 3
  • 17