Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从url解析JSON,然后将其显示给TextView_Android_Json - Fatal编程技术网

Android 从url解析JSON,然后将其显示给TextView

Android 从url解析JSON,然后将其显示给TextView,android,json,Android,Json,我想问一下JSON解析。如果我从url=http://localhost/bus/format/json: { "status":"success", "sessionid":"489GHJ8969" "schedule":{ "went":{ "description":"this is bus schedule", "bus":{ "1":"A30", "2":"A40" },

我想问一下JSON解析。如果我从url=
http://localhost/bus/format/json

{
  "status":"success",
  "sessionid":"489GHJ8969"
  "schedule":{
     "went":{
        "description":"this is bus schedule",
        "bus":{
           "1":"A30",
           "2":"A40"
        },
        "depart":{
           "1":"8AM",
           "2":"11AM"
        }
      }
    }
}
如何在Android中获取并解析此JSON,然后将其显示给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)

Android内置了JSON解析器:

它使用DOM模型(因此不适合较大的数据) 如果您需要更舒适的环境或使用gran google GSON或my small library:

看看
例如:

JSONObject object = new JSONObject(yourResponse);
String status = object.getString("status");
JSONObject schedule = object.getJSONObject("schedule");
.....

使用“如何在android中解析JSON”写入google。我在这个链接中尝试了这个示例,但我仍然不理解如何使用JSONObject和JSONArray,然后将其放到textview。因此,请帮助我:)