2013-06-21 2 views
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> 

답변

3

당신이 순서대로해야 할 여러 가지가 있습니다 ~을하다 hings work

  1. 인터넷 권한이 설정되어 있는지 확인하십시오. What permission do I need to access Internet from an android application?
  2. UI 스레드가 아닌 별도의 스레드에서만 인터넷에 연결할 수 있습니다. 당신은 UI 스레드에서 UI를 편집 할 수 있습니다
  3. (당신은 정말 doInBackground 및 onPostExecute 필요), 그래서 당신은 응답의 내용을 얻을 수 있도록 AsyncTask를
  4. 에 쉬운 방법을 onPostExecute 방법을 사용 http://developer.android.com/reference/android/os/AsyncTask.html를 참조하여 사용하는 것입니다 EntityUtils.toString (response.getEntity)
+0

감사합니다. 그것은 일했다. .. 나는 인터넷 허가를 설정하지 않았다. –

+0

문제 없음 : D. 그래도 우리에게 은혜를 베풀어주고 다른 사람들이 답변을 받았다는 것을 알기 위해 대답을 인정 된 대답으로 표시하십시오. –

0
package com.example.bucky1; 
import android.annotation.TargetApi; 
import android.app.Activity; 
import android.os.Build; 
import android.os.Bundle; 
import android.os.StrictMode; 
import android.widget.TextView; 


public class HttpExample extends Activity{ 

    TextView httpStuff; 

    @TargetApi(Build.VERSION_CODES.GINGERBREAD) 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     // ADD 2 LINES 
     StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 
     StrictMode.setThreadPolicy(policy); 
     // ------------- 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.httpex); 
     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(); 
     } 
    } 


} 
관련 문제