2012-01-10 76 views
0

我需要包括JSON在我的Android應用程序, 我按照一些教程,並安裝了該應用從Twitter讀取測試Android的JSON Web服務問題

package com.or.jsonswitch; 

import *** 

public class JsonTestMySwitchActivity extends Activity { 
/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    Log.d("msg", "app started 1"); 

    String readResponse = readResponse(); //crea un string llenado por lo que devuelve la funcion() 

    try { 
     JSONArray jsonArray = new JSONArray(readResponse); 
     Log.d("msg", "number of entries::"+jsonArray.length()); 
     Log.d("msg", "response::"+jsonArray); 

    } catch (Exception e) { 
     e.printStackTrace(); //donde va el stacktrace? 
    } 

} 

public String readResponse() { //autogenera como private!, cambio a public! 

    Log.d("msg" , "entra a buscar"); 

    StringBuilder builder = new StringBuilder(); 
    HttpClient client = new DefaultHttpClient(); 
    //HttpGet httpGet = new HttpGet("http://twitter.com/statuses/user_timeline/vogella.json"); 
    HttpGet httpGet = new HttpGet("http://myswitch.merged.stage.orchard.net.au/LifftService.svc/JSON/CoverageSearchByLatLong/-33.881393,151.214534"); 

    //httpGet.setHeader("Content-Type", "text/plain; charset=utf-8"); 
    //httpGet.setHeader("Content-Type", "json"); 


    try { 
     HttpResponse response = client.execute(httpGet); //que es? 
     StatusLine statusLine = response.getStatusLine(); //que es? 
     int statusCode = statusLine.getStatusCode(); 
     if (statusCode == 200) { //200? 
      HttpEntity entity = response.getEntity(); //que es? 
      InputStream content = entity.getContent(); //que es? 
      BufferedReader reader = new BufferedReader(new InputStreamReader(content)); //que es? 
      String line; 
      while ((line = reader.readLine()) != null) { 
       builder.append(line); 
      } 
     } else { 
      Log.d("msg" , "Failed to download file"); 
     } 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
     Log.d("msg", "client protocol exception:"+e); 
    } catch (IOException e) { 
     e.printStackTrace(); 
     Log.d("msg", "client protocol exception:"+e); 
    }  
    return builder.toString(); 
} 

注意,它取出由JSON數據時的工作原理Twitter的:

//HttpGet httpGet = new HttpGet("http://twitter.com/statuses/user_timeline/vogella.json"); 

,但顯示爲空白,從我所希望的服務器獲取時:

HttpGet httpGet = new HttpGet("http://myswitch.merged.stage.orchard.net.au/LifftService.svc/JSON/CoverageSearchByLatLong/-33.881393,151.214534"); 

當它返回空白時,我沒有錯誤消息,

我必須設置指定它的標頭是JSON嗎?怎麼樣?

//httpGet.setHeader("Content-Type", "json"); 
+0

我會開始通過對URL進行cURL調試來查看Android響應和cURL響應之間的差異。 – curioustechizen 2012-01-10 06:26:59

回答

0

發現問題,

是因爲響應不是一個數組,是一個字典,我正在作出響應一個數組,

即時通訊不在我的編碼計算機,所以現在不能郵編代碼,但那是基本上是問題。

謝謝

1

下面的代碼會給你的JSONObject然後循環從這個的JSONObject和遍歷結果採取jsonarray ...希望它會幫助你

JSONObject jobject = null; 

     //http post 
     try{ 
       HttpClient httpclient = new DefaultHttpClient(); 
       HttpPost httppost = new HttpPost("http://myswitch.merged.stage.orchard.net.au/LifftService.svc/JSON/CoverageSearchByLatLong/-33.881393,151.214534"); 
       HttpResponse response = httpclient.execute(httppost); 
       HttpEntity entity = response.getEntity(); 
       is = entity.getContent(); 

     }catch(Exception e){ 
       Log.e("log_tag", "Error in http connection "+e.toString()); 
     } 

     //convert response to string 
     try{ 
       BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8); 
       StringBuilder sb = new StringBuilder(); 
       String line = null; 
       while ((line = reader.readLine()) != null) { 
         sb.append(line + "\n"); 
       } 
       is.close(); 
       result=sb.toString(); 
     }catch(Exception e){ 
       Log.e("log_tag", "Error converting result "+e.toString()); 
     } 

     try{ 

      jobject = new JSONObject(result);    
     }catch(JSONException e){ 
       Log.e("log_tag", "Error parsing data "+e.toString()); 
     } 
+0

嗨,鏈接似乎爲我工作,但那不是問題,請閱讀我自己的答案,謝謝你 – MaKo 2012-01-10 08:32:55

+0

看到我編輯的文章 – Vamshi 2012-01-10 08:49:47