package com.example.urlget;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
private static final int s=1;
private static final int f=0;
private EditText ed;
private Button btn;
private String txt;
String urlString="http:192.168.1.120:8080/AndroidTest/Message.txt";
private Handler mHandler=new Handler(){
public void handleMessage(Message msg){
switch (msg.what) {
case s:ed.setText(txt);
break;
case f:ed.setText("Download the file Failure!");
break;
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ed=(EditText) findViewById(R.id.ed);
btn=(Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
GetURlResources();
}
});
}
private void GetURlResources() {
// TODO Auto-generated method stub
new Thread(new Runnable() {
@Override
public void run() {
try {
Log.v("log", "hello");
URL myUrl=new URL(urlString);
HttpURLConnection myConn=(HttpURLConnection) myUrl.openConnection();
Log.v("log", "hello1");
myConn.setConnectTimeout(3000);
Log.v("log", "hello2");
InputStreamReader in=new InputStreamReader(myConn.getInputStream());
Log.v("log", "hello3");
BufferedReader buffer=new BufferedReader(in);
String inputline =null;
StringBuffer pageBuffer=new StringBuffer();
Log.v("log", "hello4");
while((inputline=buffer.readLine())!=null){
pageBuffer.append(inputline+"\n");
}
Log.v("log", "hello5");
txt=new String(pageBuffer.toString().getBytes("UTF-8"));
txt=txt.substring(0,txt.length()-1);
mHandler.sendEmptyMessage(0);
in.close();
buffer.close();
myConn.disconnect();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
}
这个在手机上运行logcat怎么显示 E/dalvikvm(20427): threadid=11: exiting,name=Thread-5433新手求大神指点