Android中的HttpClient无法正常工作

时间:2013-06-21 21:32:00

标签: android httprequest

我是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>

2 个答案:

答案 0 :(得分:3)

为了让事情有效,你需要做很多事情

  1. 确保您已设置互联网权限。请参阅What permission do I need to access Internet from an android application?
  2. 您只能在不是UI线程的单独线程上连接到Internet。请参阅http://developer.android.com/reference/android/os/AsyncTask.html(您只需要doInBackground和onPostExecute)
  3. 您只能编辑UI线程上的UI,因此请在AsyncTask中使用onPostExecute方法
  4. 获取响应内容的更简单方法是使用EntityUtils.toString(response.getEntity)

答案 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();
        }
    }


}