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?

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

how can i use it after my code InputStream benimin = new BufferedInputStream(benimconnection.getInputStream());
@user2778565 The link in my answer provides details on how to get the contents of an InputStream into a String. Please read it carefully.
0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.