[英]android application to fetch internet data
這是當我運行此代碼時從互聯網bu提取數據的android代碼
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
。 使用Thread
或Asynctask
。
http://developer.android.com/reference/android/os/AsyncTask.html
如果網絡操作持續時間較短,請使用Asynctask
。
您可以Asynctask
的內部類中更新UI onPostExecute
。
還要遵循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);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.