이것은 내가 데이터가안드로이드 응용 프로그램은
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을 표시되지 않습니다이 코드를 실행하면 안드로이드 코드가 인터넷 BU에서 데이터를 가져 오는 것입니다 파일
<?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>
당신이 로그 캣에 오류가 있습니까를? API 레벨이 HONEYCOMB보다 높으면 다른 스레드에서 모든 네트워크 항목을 실행해야합니다. – nikis
메인 스레드에서 인터넷에 연결할 수 없기 때문에'String v = ob.a();'에서 예외를 가져와야합니다. AsycTask 클래스 또는 그 스레드를 수행하는 –