2013-08-30 59 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