android应用程序获取互联网数据

android应用程序获取互联网数据,android,Android,这是android代码,用于从internet bu获取数据。当我运行此代码时,不会显示任何数据 public class data { public String a()throws Exception { String data=null; BufferedReader b=null; try { HttpClient ob=new DefaultHttpClient

这是android代码,用于从internet bu获取数据。当我运行此代码时,不会显示任何数据

public class data 
    {

    public String a()throws Exception

    {
        String data=null;

        BufferedReader b=null;

        try

        {

            HttpClient ob=new DefaultHttpClient();

            URI website=new URI("http://www.mybringback.com");

            HttpGet request=new HttpGet();

            request.setURI(website);

            HttpResponse response= ob.execute(request);

            b=new BufferedReader(new InputStreamReader(response.getEntity().getContent()));

            StringBuffer bu=new StringBuffer ();

            String i = null;

            while((i=b.readLine())!=null)
            {
                bu.append(i);
            }

            b.close();

            data=bu.toString();

            return data;
        }finally
        {
            if (b!=null)
            {
                try {
                    b.close();

                    return data;


                }catch(Exception e)
                {
                    e.printStackTrace();
                }
            }
        }
        }
}
我的主课

package com.example.hello;


import android.app.Activity;

import android.os.Bundle;

import android.widget.TextView;

import org.apache.http.HttpEntity;

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;

import java.io.*;

import java.net.URI;

public class MyActivity extends Activity
 {

@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);


    TextView m =(TextView)findViewById(R.id.de);

    data ob=new data();

    try {
        String v= ob.a();

        m.setText(v);

    } catch (Exception e) {

        e.printStackTrace();
    }
  }
 }
我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent">
<ScrollView 
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent">
 <TextView
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:text="Hello World, MyActivity"
    android:id="@+id/de">

 </TextView>
 </ScrollView>
</LinearLayout>

您正在ui线程上运行与网络相关的操作。可能正在获取
NetworkOnMainThreadException
。使用
线程
异步任务

如果网络操作持续时间较短,请使用
Asynctask

您可以在
onPostExecute
中将
Asynctask
内部类作为更新ui

还要遵循java命名约定,重新命名变量和方法,这有助于可读性

例如:

public class MyActivity extends Activity
{
TextView m;  
@Override
public void onCreate(Bundle savedInstanceState)
{
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   m=(TextView)findViewById(R.id.de);
   new TheTask().execute()
}

class TheTask extends AsyncTask<Void,Void,String>
{
   String  _response=null;   
@Override
protected String doInBackground(Void... params) {
    // TODO Auto-generated method stub
     try
     {
       HttpClient ob=new DefaultHttpClient();
       URI website=new URI("http://www.mybringback.com");
       HttpGet request=new HttpGet();
       request.setURI(website);
       HttpResponse response= ob.execute(request);
       HttpEntity resEntity = response.getEntity();
       _response=EntityUtils.toString(resEntity); 
     }catch(Exception e)
     {
         e.printStackTrace();
     }
    return _response;
}
@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
            m.setText(result);
}
    }
}
公共类MyActivity扩展活动
{
文本视图m;
@凌驾
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m=(TextView)findViewById(R.id.de);
新建任务().execute())
}
类任务扩展异步任务
{
字符串_response=null;
@凌驾
受保护字符串doInBackground(无效…参数){
//TODO自动生成的方法存根
尝试
{
HttpClient ob=新的DefaultHttpClient();
URI网站=新的URI(“http://www.mybringback.com");
HttpGet请求=新建HttpGet();
setURI(网站);
HttpResponse response=ob.execute(请求);
HttpEntity当前性=response.getEntity();
_response=EntityUtils.toString(resEntity);
}捕获(例外e)
{
e、 printStackTrace();
}
返回响应;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//TODO自动生成的方法存根
super.onPostExecute(结果);
m、 setText(结果);
}
}
}

logcat中是否有任何错误?如果您的API级别高于HONEYCOMB,您应该在另一个线程上运行所有网络内容,您必须在
String v=ob.a()上获得异常
因为您不能在主线程上连接到Internet,所以必须使用AsycTask类或线程为
执行+1操作,遵循java命名约定并重新命名变量和方法,这有助于可读性