2012-10-12 75 views
0

我想問一下關於JSON解析。如果我有這樣的URL Web服務= http://localhost/bus/format/json從URL解析JSON然後將其顯示到TextView

{ 
    "status":"success", 
    "sessionid":"489GHJ8969" 
    "schedule":{ 
    "went":{ 
     "description":"this is bus schedule", 
     "bus":{ 
      "1":"A30", 
      "2":"A40" 
     }, 
     "depart":{ 
      "1":"8AM", 
      "2":"11AM" 
     } 
     } 
    } 
} 

如何獲得和解析此JSON在Android中則顯示出它的TextView?我在這裏搜索,但我仍然很困惑如何解析JSON。

這是更新後,我試圖解析JSON,但仍沒有關於如何解析JSON

public class JSONParser { 

static InputStream is = null; 
static JSONObject jObj = null; 
static String json = ""; 

// constructor 
public JSONParser() { 

} 

public JSONObject getJSONFromUrl(String url) { 

    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 

     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent();   

    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 

    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(); 
     json = sb.toString(); 
    } catch (Exception e) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 

    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 

    // return JSON String 
    return jObj; 

    } 
} 

總線活動

public class Bus extends Activity { 

String url, s; 

    //private static final String TAG_STATUS = "Status"; 
private static final String TAG_SCHEDULE = "schedule"; 
private static final String TAG_WENT = "went"; 
private static final String TAG_DESCRIPTION = "description"; 

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

    TextView tx = (TextView)findViewById(R.id.textView1); 

    url = "http://localhost/bus/format/json"; 

    tx.setText(url); 

    JSONParser jParser = new JSONParser(); 

    JSONObject json = jParser.getJSONFromUrl(url); 

    try { 
     JSONObject jadwal = json.getJSONObject(TAG_SCHEDULE); 
     JSONObject went = jadwal.getJSONObject(TAG_WENT); 
     s = went.getString(TAG_DESCRIPTION); 

    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
}  
} 

,然後將結果

10-13 00:51:15.212: W/System.err(373): Caused by: java.net.ConnectException: /127.0.0.1:80 - Connection refused 
10-13 00:51:15.212: W/System.err(373): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:254) 
10-13 00:51:15.212: W/System.err(373): at org.apache.harmony.luni.net.PlainSocketImpl.connect(PlainSocketImpl.java:533) 
10-13 00:51:15.212: W/System.err(373): at java.net.Socket.connect(Socket.java:1055) 
10-13 00:51:15.222: W/System.err(373): at org.apache.http.conn.scheme.PlainSocketFactory.connectSocket(PlainSocketFactory.java:119) 
10-13 00:51:15.222: W/System.err(373): at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:143) 
10-13 00:51:15.222: W/System.err(373): ... 21 more 
10-13 00:51:15.222: E/Buffer Error(373): Error converting result java.lang.NullPointerException 
10-13 00:51:15.222: E/JSON Parser(373): Error parsing data org.json.JSONException: End of input at character 0 of 
10-13 00:51:15.222: D/AndroidRuntime(373): Shutting down VM 
10-13 00:51:15.222: W/dalvikvm(373): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
+0

使用[JSONObject的(http://developer.android.com/reference/org/json/JSONObject.html) – m0skit0

+2

寫的 「如何解析JSON android的」 到谷歌。 –

+0

我曾試過這個鏈接http://www.androidhive.info/2012/01/android-json-parsing-tutorial/中的例子,但我仍然不明白如何使用JSONObject和JSONArray,然後把它放到textview .so請幫助我:) –

回答