0

我是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();
      }
    }



}

获取方法.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>
4

2 回答 2

3

为了使事情正常进行,您需要做很多事情

  1. 确保您已设置 Internet 权限。请参阅从 Android 应用程序访问 Internet 需要什么权限?
  2. 您只能在不是 UI 线程的单独线程上连接到 Internet。参考http://developer.android.com/reference/android/os/AsyncTask.html(你只需要doInBackground和onPostExecute)
  3. 您只能在 UI 线程上编辑 UI,因此请在 AsyncTask 中使用 onPostExecute 方法
  4. 获取响应内容的更简单方法是使用 EntityUtils.toString(response.getEntity)
于 2013-06-21T22:21:41.440 回答
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();
        }
    }


}
于 2014-10-26T22:20:54.633 回答