0
저는 안드로이드 개발에 초보자입니다. 나는 인터넷에서 일부 데이터를 얻으려고하고 있는데 그것을 표시하려고합니다. 프로그램을 실행할 때 빈 페이지가 표시되고 오류가 표시되지 않습니다.안드로이드의 HttpClient가 제대로 작동하지 않습니다.
감사
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>
감사합니다. 그것은 일했다. .. 나는 인터넷 허가를 설정하지 않았다. –
문제 없음 : D. 그래도 우리에게 은혜를 베풀어주고 다른 사람들이 답변을 받았다는 것을 알기 위해 대답을 인정 된 대답으로 표시하십시오. –