2012-01-10 16 views
0
私がアンドロイドアプリでJSONを含める必要が

、 は、私はいくつかのチュートリアルを踏襲してからJSONデータを取得するとき、それが動作することを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(); 
} 

注テストを読んでアプリを持っていますツイッター:

//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

感謝。 – curioustechizen

答えて

0

を助けることを願っていますので、問題を発見した

です応答は配列ではなく、辞書であり、応答を配列にしていました。

私のコーディングコンピュータにはまだありません。基本的に問題。私はあなたのURLにカールを行うことで、このデバッグを開始し、Androidの応答とcURLの応答の違いを参照してくださいね

1

、ループの結果を通じて、このjsonobjectとループからjsonarrayを取るあなたのJSONObjectを与えるだろう...それはあなたの

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

+0

私の編集後の記事を見る – Vamshi