我如何填充使用JSON Web服务的Andr​​oid的微调? [英] How do I populate an Android spinner using a JSON web service?

查看:124
本文介绍了我如何填充使用JSON Web服务的Andr​​oid的微调?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何解析如下图所示包含学校名称和ID,并使用在名称字段中的值来填充飞旋标贴的一个JSON文件?

我要微调输出选择SchoolID到下一个活动。例如。所以你看山景小学,如果你选择这个值HVE输出到下一个活动采取行动。

  {
学校:
{
名:山景小学,
SchoolID:HVE},
{
名:穆勒查看,
SchoolID:MVE},
{
名:大学校,
SchoolID:BSC},
]
}
 

下面是我用来尝试读取饲料中的onCreate ...

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    串readFeed = readFeed();
    尝试 {
      JSONArray jsonArray =新
      JSONArray(readFeed);
      Log.i(MainActivity.class.getName(),
          条目数+ jsonArray.length());

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

        //这里,而不是记录我想用每个学校名称为
        //投入,我可以用它来填充字符串数组
        //为微调

        Log.i(MainActivity.class.getName(),jsonObject.getString(学校));

      }
    }赶上(例外五){
      e.printStackTrace();
    }
}
 

我还是来了,以加速在Android上那么清晰AP preciated。

下面是readfeed:

 公共字符串readFeed(){
    StringBuilder的建设者=新的StringBuilder();
    HttpClient的客户端=新DefaultHttpClient();

    //域故意混淆出于安全原因
    HTTPGET HTTPGET =新HTTPGET(http://www.domain.com/schools.php);
    尝试
 {
      HTT presponse响应= client.execute(HTTPGET);
      状态行状态行= response.getStatusLine();
      INT状态code = statusLine.getStatus code();
      如果(状态code == 200){
        HttpEntity实体= response.getEntity();
        InputStream的内容= entity.getContent();
        的BufferedReader读卡器=新的BufferedReader(新InputStreamReader的(内容));
        串
 线;
        而((行= reader.readLine())!= NULL){
          builder.append(线);
        }
      } 其他 {
        Log.e(MainActivity.class.toString(),无法下载文件);
      }
    }赶上(ClientProtocolException E){
      e.printStackTrace();
    }赶上(IOException异常E)
 {
      e.printStackTrace();
    }
    返回builder.toString();
  }
 

解决方案

我会做这样的事情:

 公共类学校{
    私人字符串名称;
    私人字符串ID;

    公共字符串的getName(){
        返回名称;
    }
    公共无效setname可以(字符串名称){
        this.name =名称;
    }
    公共字符串的getId(){
        返回ID;
    }
    公共无效SETID(字符串ID){
        this.id = ID;
    }
}
 

在创建:

  @覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.activity_main);

    串readFeed = readFeed();

    //你可以使用这个数组查找基于名字的学生证
    ArrayList的<学校>学校=新的ArrayList<学校>();
    //你可以使用这个数组来填充微调
    ArrayList的<字符串> schoolNames =新的ArrayList<字符串>();

    尝试 {
      JSONObject的JSON =新的JSONObject(readFeed);
      JSONArray jsonArray =新JSONArray(json.optString(学校));
      Log.i(MainActivity.class.getName(),
          条目数+ jsonArray.length());

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

        校舍=新学校();
        school.setName(jsonObject.optString(名称));
        school.setId(jsonObject.optString(SchoolID));
        schools.add(学校);
        schoolNames.add(jsonObject.optString(名称));

      }
    }赶上(例外五){
      e.printStackTrace();
    }
    微调mySpinner =(微调)findViewById(R.id.my_spinner);
    mySpinner.setAdapter(新ArrayAdapter<字符串>(这一点,android.R.layout.simple_spinner_dropdown_item,schoolNames));
}
 

在你main_activity.xml你需要一个微调。在code以上将与一个名为像下面微调:

 <微调
    机器人:ID =@ + ID / my_spinner
    机器人:layout_height =WRAP_CONTENT
    机器人:layout_width =WRAP_CONTENT/>
 

How do I parse a JSON file like the one below containing school names and IDs and use the values in the "Name" field to populate the spinner lables?

I want the spinner to output the selected SchoolID to the next activity. E.g. So you see "Hill View Elementary" and if you select this the value "HVE" is output to the next activity to act on.

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

Here is the onCreate I'm using to attempt to read the feed...

@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();
    }
}

I'm still coming up to speed on Android so clarity appreciated.

Here is readfeed:

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();
  }

解决方案

I would do something like this:

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;
    }
}

On create:

@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));
}

In your main_activity.xml you need a Spinner. The code above would work with a Spinner named like below:

<Spinner
    android:id="@+id/my_spinner"
    android:layout_height="wrap_content"
    android:layout_width="wrap_content"/>

这篇关于我如何填充使用JSON Web服务的Andr​​oid的微调?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆