如何在Android中将Web API的结果显示到TextView中

时间:2014-05-02 07:03:55

标签: android json web-services

我是新手android开发者。我试图在我的示例应用程序中点击按钮调用webAPI,我成功获得了android格式的响应,但我无法在android中的TextView中显示这些值。

这是我的代码..

private void getdata() {
    try {
        StrictMode.ThreadPolicy policy = new StrictMode.
          ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy); 
        URL url = new URL("http://pnrbuddy.com/api/check_pnr/pnr/2156641988/format/json/pbapikey/6b17f33e25e2d8197462d1c6bcb0b130/pbapisign/bd0aea241e88c8a22692eba02887ad97a220f827");
        HttpURLConnection con = (HttpURLConnection) url
          .openConnection();
        readStream(con.getInputStream());
    } catch (Exception e) {
        e.printStackTrace();
    }
}     

private void readStream(InputStream in) {
  BufferedReader reader = null;
  try {
    reader = new BufferedReader(new InputStreamReader(in));
    String line = "";
    while ((line = reader.readLine()) != null) {
      System.out.println(line);
    }
  } catch (IOException e) {
    e.printStackTrace();
  } finally {
    if (reader != null) {
      try {
        reader.close();
      } catch (IOException e) {
        e.printStackTrace();
      }
    }
  }
} 

这是我需要在TextView中显示的API示例输出数据..

{"response_code":200,"pnr":"2156641988","train_num":"14208","train_name":"PADMAVAT          EXPRES","doj":"2014-05-17","from_station":{"code":"DLI ","name":"Delhi"},"to_station":{"code":"PBH ","name":"Partapgarh Junction"},"reservation_upto":{"code":"PBH ","name":"Partapgarh Junction"},"boarding_point":{"code":"DLI ","name":"Delhi"},"class":" SL","no_of_passengers":2,"chart_prepared":"N","passengers":[{"sr":"1","booking_status":"S4,38,GN","current_status":"CNF"},{"sr":"2","booking_status":"S4,40,GN","current_status":"CNF"}],"error":null}

我需要在我的应用中显示pnr,train_num,train_name

请帮帮我..

这是我的.xml设计代码..

 <LinearLayout android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@drawable/bg"
    android:orientation="vertical"
    android:padding="30dp" xmlns:android="http://schemas.android.com/apk/res/android">

    <TextView
        android:id="@+id/TextView02"
        android:layout_width="213dp"
        android:layout_height="wrap_content"
        android:text="Welcome !"
        android:textColor="#ffffff"
        android:textStyle="bold" />

    <TextView
        android:id="@+id/TextView01"
        android:layout_width="213dp"
        android:layout_height="wrap_content"
        android:text="Vikas Technologies Railinfo"
        android:textColor="#ffffff" />

    <Space
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <GridLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:columnCount="5" >

        <Button
            android:id="@+id/button1"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="109dp"
            android:layout_column="0"
            android:layout_columnSpan="4"
            android:layout_gravity="left"
            android:layout_row="0"
            android:text="Check PNR"
            android:textColor="#ffffff" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="112dp"
            android:layout_column="4"
            android:layout_gravity="left"
            android:layout_row="0"
            android:text="Train Enquiry"
            android:textColor="#ffffff" />

        <TextView
            android:id="@+id/textView1"
            android:layout_column="2"
            android:layout_columnSpan="2"
            android:layout_gravity="left"
            android:layout_row="2"
            android:text="Enter PNR"
            android:textColor="#ffffff" />

        <EditText
            android:id="@+id/editText1"
            android:layout_column="1"
            android:layout_columnSpan="4"
            android:layout_gravity="left"
            android:layout_row="4"
            android:ems="10"
            android:inputType="textEmailAddress"
            android:textColor="#ffffff"
            android:color="#ffffff" >

            <requestFocus />
        </EditText>

        <Button
            android:id="@+id/button3"
            android:layout_height="wrap_content"
            android:layout_column="3"
            android:layout_columnSpan="2"
            android:layout_gravity="left"
            android:layout_row="5"
            android:text="Submit"
            android:textColor="#ffffff" />

        <Space
            android:layout_width="46dp"
            android:layout_height="1dp"
            android:layout_column="4"
            android:layout_row="0" />

        <Space
            android:layout_width="1dp"
            android:layout_height="58dp"
            android:layout_column="0"
            android:layout_row="0" />

        <Space
            android:layout_width="1dp"
            android:layout_height="14dp"
            android:layout_column="0"
            android:layout_row="1" />

        <Space
            android:layout_width="1dp"
            android:layout_height="10dp"
            android:layout_column="0"
            android:layout_row="3" />

        <Space
            android:layout_width="1dp"
            android:layout_height="10dp"
            android:layout_column="0"
            android:layout_row="5" />

        <Space
            android:layout_width="1dp"
            android:layout_height="166dp"
            android:layout_column="0"
            android:layout_row="6" />

        <Space
            android:layout_width="21dp"
            android:layout_height="1dp"
            android:layout_column="0"
            android:layout_row="0" />

        <Space
            android:layout_width="47dp"
            android:layout_height="1dp"
            android:layout_column="1"
            android:layout_row="0" />

        <Space
            android:layout_width="16dp"
            android:layout_height="1dp"
            android:layout_column="2"
            android:layout_row="0" />

        <Space
            android:layout_width="47dp"
            android:layout_height="1dp"
            android:layout_column="3"
            android:layout_row="0" />
    </GridLayout>

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

尝试以下代码: -

                try
                {
                    JSONObject j = new JSONObject("response form server");
                    textView.setText(j.getString("pnr")+"\n"+
                            j.getString("train_num")+"\n"+
                            j.getString("train_name"));
                }
                catch (Exception e)
                {
                    // TODO: handle exception
                    e.printStackTrace();
                }

答案 1 :(得分:0)

嗨,你的json数据如下

{
  "response_code": 200,
  "pnr": "2156641988",
  "train_num": "14208",
  "train_name": "PADMAVAT          EXPRES",
  "doj": "2014-05-17",
  "from_station": {
    "code": "DLI ",
    "name": "Delhi"
  },
  "to_station": {
    "code": "PBH ",
    "name": "Partapgarh Junction"
  },
  "reservation_upto": {
    "code": "PBH ",
    "name": "Partapgarh Junction"
  },
  "boarding_point": {
    "code": "DLI ",
    "name": "Delhi"
  },
  "class": " SL",
  "no_of_passengers": 2,
  "chart_prepared": "N",
  "passengers": [
    {
      "sr": "1",
      "booking_status": "S4,38,GN",
      "current_status": "CNF"
    },
    {
      "sr": "2",
      "booking_status": "S4,40,GN",
      "current_status": "CNF"
    }
  ],
  "error": null
}

请提供以下解析代码

String jsondata = "YOURS DATA FROM SERVER IN JSON FORMAT";
        JSONObject primaryObject = new JSONObject(jsondata);

        String responseCode = primaryObject.getString("response_code");
        String pnr = primaryObject.getString("pnr");
        String trainNum = primaryObject.getString("train_num");
        String date = primaryObject.getString("doj");
        String trainName = primaryObject.getString("train_name");

        JSONObject from_station = primaryObject.getJSONObject("from_station");
        String fromcode = from_station.getString("code");
        String fromname = from_station.getString("name");

        JSONObject to_station = primaryObject.getJSONObject("to_station");
        String tocode = to_station.getString("code");
        String toname = to_station.getString("name");

        JSONObject reservation_upto = primaryObject
                .getJSONObject("reservation_upto");
        String uptocode = reservation_upto.getString("code");
        String uptoname = reservation_upto.getString("name");

        JSONObject boarding_point = primaryObject
                .getJSONObject("boarding_point");
        String boardtocode = boarding_point.getString("code");
        String boardtoname = boarding_point.getString("name");

        String classvalue = primaryObject.getString("class");

        String noofpass = primaryObject.getString("no_of_passengers");

        String chartstatus = primaryObject.getString("chart_prepared");

        String error = primaryObject.getString("error");

        JSONArray passengers = primaryObject.getJSONArray("passengers");

        for (int i = 0; i < passengers.length(); i++) {
            JSONObject firstObj = passengers.getJSONObject(i);

            String sr = firstObj.getString("sr");

            String booking_status = firstObj.getString("booking_status");

            String current_status = firstObj.getString("current_status");
        }