我正在尝试开发一个可以连接到 Internet 并从指定网站读取数据的 Android 应用程序。但是,当我在模拟器上运行程序时,什么也没有发生。有人可以帮我指导我需要做什么吗?这是我的代码
public class HttpExaplme extends Activity {
TextView httpStuff;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.httpex);
httpStuff = (TextView) findViewById(R.id.tv_http);
GetHttpEx test = new GetHttpEx();
String returned;
try {
returned = test.getInternetData();
httpStuff.setText(returned);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public class GetHttpEx {
public String getInternetData() throws Exception
{
BufferedReader in = null;
String data = null;
try{
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.mybringback.com");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l ="";
String nl= System.getProperty("line sperator");
while((l = in.readLine())!=null)
{
sb.append(l + nl);
}
in.close();
data = sb.toString();
return data;
}
finally
{
try{
if(in != null)
{
in.close();
return data;
}
}
catch(Exception e){
e.printStackTrace();
}
}
}