我是Android开发的新手。我只是想从互联网上获取一些数据并希望显示它。我运行程序时显示一个空白页面,没有出现错误。
谢谢
HttpExample.java
package com.example.bucky1;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class HttpExample extends Activity {
TextView httpStuff;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.httpexample);
httpStuff =(TextView)findViewById(R.id.tvhttp);
GetMethodEx getdata = new GetMethodEx();
String returned;
try
{
returned = getdata.getInternetData();
httpStuff.setText(returned);
}
catch(Exception e){
e.printStackTrace();
}
}
}
GetMethod.java
package com.example.bucky1;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URI;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
public class GetMethodEx {
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()));
String l ="";
String nl = System.getProperty("line.seprator");
StringBuffer sb = new StringBuffer("");
while((l = in.readLine()) != null )
{
sb.append(l + nl);
} in.close();
data = sb.toString();
return data;
}
finally{
if(in != null)
{
try{
in.close();
return data;
}
catch(Exception e) {
e.printStackTrace();
}
}
}
}
}
httpexample.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/tvhttp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</ScrollView>
</LinearLayout>
答案 0 :(得分:3)
为了让事情有效,你需要做很多事情
答案 1 :(得分:0)
package com.example.bucky1;
import android.annotation.TargetApi;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.TextView;
public class HttpExample extends Activity{
TextView httpStuff;
@TargetApi(Build.VERSION_CODES.GINGERBREAD)
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
// ADD 2 LINES
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
// -------------
super.onCreate(savedInstanceState);
setContentView(R.layout.httpex);
httpStuff = (TextView) findViewById(R.id.tvHttp);
GetMethodEx test = new GetMethodEx();
String returned;
try {
returned = test.getInternetData();
httpStuff.setText(returned);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}