我正在尝试以最简单的方式连接任何网页。我了解到我必须使用 AsyncTask 但我想在此之前看到这项工作。
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;
}
}
当我单击按钮时,我的字符串更改为 'Java.io.BufferedInputStream@4177b2a8' 而不是我的网页输出。怎么了?