2012-05-09 37 views
0

我是android新手,對代碼的理解非常有限。我正試圖從互聯網上的api返回xml數據。我已經閱讀了幾篇關於此的文章,但沒有一篇似乎能夠工作,並且它們對代碼的解釋很少,所以我無法對它進行太多的修改。android,使用httpget返回xml數據

任何人都可以請解釋我哪裏錯了,並給我一點指導什麼需要改變?

乾杯傢伙

package com.example.testingXML; 

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.URI; 
import java.net.URISyntaxException; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.entity.BufferedHttpEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.app.Activity; 
import android.os.Bundle; 
import android.util.Log; 

public class Main extends Activity { 
    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    try { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpGet httpget = new HttpGet(); 
     httpget.setURI(new URI("http://api.inapub.co.uk/venues/postcode/bn23fe/1/?API_Key=7xa3tdmjkhu6jwjvp746zgg4")); 


     HttpResponse response = httpclient.execute(httpget); 
     HttpEntity ht = response.getEntity(); 

     BufferedHttpEntity buf = new BufferedHttpEntity(ht); 

     InputStream is = buf.getContent(); 

     BufferedReader r = new BufferedReader(new InputStreamReader(is)); 

     StringBuilder total = new StringBuilder(); 
     String line; 
     String NL = System.getProperty("line.separator"); 
     while((line = r.readLine()) != null){ 
     total.append(line + NL); 
     } 

     r.close(); 
     String page = r.toString(); 

    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } catch (URISyntaxException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 
} 
+0

你得到的總響應確保您的應用程序請求互聯網權限

​​

3)要做到這一點,最簡單的方法是什麼? log.v( 「testest」,總計);檢查它是否得到api響應或不響應? –

回答