2013-08-30 58 views
0

我是所有這些android編程的新手。遵循幾個教程後,我成功解析了一個JSON url。基本上,我想要做的是將我得到的字符串(ipString)打印爲textview或listview。如何將字符串打印爲ListView

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    Log.d("Rami","Rami"); 
    DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams()); 
    HttpPost httppost = new HttpPost("http://jsonip.com"); 
    // Depends on your web service 
    httppost.setHeader("Content-type", "application/json"); 

    InputStream inputStream = null; 
    String result = null; 
    try { 
     HttpResponse response = httpclient.execute(httppost);   
     HttpEntity entity = response.getEntity(); 

     inputStream = entity.getContent(); 
     // json is UTF-8 by default 
     BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 
     while ((line = reader.readLine()) != null) 
     { 
      sb.append(line + "\n"); 

     } 
     result = sb.toString(); 
     JSONObject jObject = new JSONObject(result); 
     String ipString = jObject.getString("ip"); 
     Log.d("ip", ipString); 
    } catch (Exception e) { 
     // Oops 
    } 
    finally { 
     try{if(inputStream != null)inputStream.close();}catch(Exception squish){} 
    } 




} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

} 從那裏,我該怎麼辦?

感謝

+0

你沒有任何分析,你得到了原始的字符串被髮送到您的 – tyczj

回答

0

注意:在 「JSON」 由URL http://jsonip.com是無效的(根據上http://jsonlint.com/測試)返回。

它顯示了這一點:

{ 
    ip: "216.49.181.254", 
    about: "/about", 
    Pro!: "http://getjsonip.com" 
} 

而實際上應該是這樣的:

{ 
    "ip": "216.49.181.254", 
    "about": "/about", 
    "Pro!": "http://getjsonip.com" 
} 

爲了簡化您的JSON從網上獲取,我會建議(AQuery)看着AndroidQuery 。它有一個簡單的JSON調用,它允許您打印出結果,或者在返回時遍歷結果。

通過這個AQuery庫,如果您已經在AsyncTask中,也可以同步獲取JSON。

這裏是取的JSON的一些示例代碼:

public void asyncJson(){ 

    //perform a Google search in just a few lines of code 

    String url = "http://www.google.com/somesearch";    
    aq.ajax(url, JSONObject.class, this, "jsonCallback"); 

} 

public void jsonCallback(String url, JSONObject json, AjaxStatus status){ 

    if(json != null){ 
     //successful ajax call 

     String ipString = ""; 

     try { 
      ipString = json.getString("ip"); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 

     Log.d("ip", ipString);//do this if you just want to log the ipString 

     //or, do this to set the "ip" string into a TextView 
     //referenced by "R.id.mytextview" 
     aq.id(R.id.mytextview).text(ipString); //this is shorthand AQuery syntax 
    } else {   
     //ajax error 
    } 

} 
+0

爲什麼是更簡單? –

+0

使用AQuery,您不必擔心HttpClient,HttpResponse,InputStream,BufferedReader等。而且,如果您處於UI線程中,它將爲您處理異步部分。 – MiStr