2

如何解析如下包含学校名称和 ID 的 JSON 文件,并使用“名称”字段中的值填充微调器标签?

我希望微调器将选定的 SchoolID 输出到下一个活动。例如,您会看到“Hill View Elementary”,如果您选择此选项,则值“HVE”将输出到下一个要执行的活动。

{
"schools": [
{
"Name": "Hill View Elementary",
"SchoolID": "HVE"},
{
"Name": "Mill View",
"SchoolID": "MVE"},
{
"Name": "Big School",
"SchoolID": "BSC"},
]
}

这是我用来尝试阅读提要的 onCreate ......

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

    String readFeed = readFeed();
    try {
      JSONArray jsonArray = new
      JSONArray(readFeed);
      Log.i(MainActivity.class.getName(),
          "Number of entries " + jsonArray.length());

      for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);

        // Here instead of Logging I want to use each schools "Name" to 
        // be put into an array that I can use to populate the strings 
        // for the spinner

        Log.i(MainActivity.class.getName(), jsonObject.getString("schools"));

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

我仍然在 Android 上加快速度,所以很清楚。

这是阅读提要:

public String readFeed() {
    StringBuilder builder = new StringBuilder();
    HttpClient client = new DefaultHttpClient();

    // domain intentionally obfuscated for security reasons
    HttpGet httpGet = new HttpGet("http://www.domain.com/schools.php");
    try 
 {
      HttpResponse response = client.execute(httpGet);
      StatusLine statusLine = response.getStatusLine();
      int statusCode = statusLine.getStatusCode();
      if (statusCode == 200) {
        HttpEntity entity = response.getEntity();
        InputStream content = entity.getContent();
        BufferedReader reader = new BufferedReader(new InputStreamReader(content));
        String
 line;
        while ((line = reader.readLine()) != null) {
          builder.append(line);
        }
      } else {
        Log.e(MainActivity.class.toString(), "Failed to download file");
      }
    } catch (ClientProtocolException e) {
      e.printStackTrace();
    } catch (IOException e)
 {
      e.printStackTrace();
    }
    return builder.toString();
  }
4

1 回答 1

13

我会做这样的事情:

public class School {
    private String name;
    private String id;

    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
}

创建时:

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

    String readFeed = readFeed();

    // you can use this array to find the school ID based on name
    ArrayList<School> schools = new ArrayList<School>();
    // you can use this array to populate your spinner
    ArrayList<String> schoolNames = new ArrayList<String>();

    try {
      JSONObject json = new JSONObject(readFeed);
      JSONArray jsonArray = new JSONArray(json.optString("schools"));
      Log.i(MainActivity.class.getName(),
          "Number of entries " + jsonArray.length());

      for (int i = 0; i < jsonArray.length(); i++) {
        JSONObject jsonObject = jsonArray.getJSONObject(i);

        School school = new School();
        school.setName(jsonObject.optString("Name"));
        school.setId(jsonObject.optString("SchoolID"));
        schools.add(school);
        schoolNames.add(jsonObject.optString("Name"));

      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    Spinner mySpinner = (Spinner)findViewById(R.id.my_spinner);
    mySpinner.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, schoolNames));
}

在您的 main_activity.xml 中,您需要一个Spinner. 上面的代码可以使用如下命名的 Spinner:

<Spinner
    android:id="@+id/my_spinner"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>
于 2012-10-16T03:09:13.743 回答