아파치 HTTPClient를 사용하여 일부 문자열 데이터를 가져 오는 간단한 방법을 썼지 만, 휴대 전화에서 실행할 때 빈 화면을 반환합니다. 텍스트 영역에 데이터를 표시하려고합니다. 매니페스트에 인터넷 사용 권한이 있습니다. 아무도 내가 잘못하고있는 것을 지적 할 수 있습니까? 안드로이드 앱의 HTTP 클라이언트가 빈 화면을 반환합니다.
public String getInternetData() throws Exception
{
BufferedReader in = null;
String data = null;
try
{
HttpClient client = new DefaultHttpClient();
URI website = new URI("http://www.yahoo.com");
HttpGet request = new HttpGet();
request.setURI(website);
HttpResponse response = client.execute(request);
in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer("");
String l = "";
String nl = System.getProperty("line.separator");
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();
}
}
}
}
완전성에 대해
, 나는 내가 데이터를 확인하는 데 사용하는 코드를 추가하고있다.
public class MainActivity extends Activity {
TextView httpStuff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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();
}
}
}
HttpClient가 Http를 통해 다운로드하는 방법 일 뿐이라는 것을 알고 있습니까? 웹뷰를 찾고 있다고 생각합니다 – Codeman
데이터를 표시하는 코드를 보여주십시오. – wyoskibum
예. 나는 그 목적을 이해한다고 믿는다. 이미 webviews를 멋지게 사용할 수 있습니다. 그러나 그것은 내가 여기서 찾고있는 것이 아닙니다. 이 메서드를 실행하면 내 메서드의 데이터 문자열이 비어서는 안되며 일부 텍스트가 표시되어야한다고 생각합니다. 그러나 나는 그렇게하지 않는다. 그래서 나는 내가 무엇을 잘못하고 있는지 궁금해하고있다. – Ammar