0

私はアンドロイドが初めてです。JSON 解析用のコードを作成します。このコードでは、JSON Web サービスを繰り返し呼び出す必要があります。私を助けてください

コード:

public class MainActivity extends Activity implements OnItemClickListener {

    private static final String rssFeed = "https://www.dropbox.com/s/rhk01nqlyj5gixl/jsonparsing.txt?dl=1";

    private static final String ARRAY_NAME = "student";
    private static final String ID = "id";
    private static final String NAME = "name";
    private static final String CITY = "city";
    private static final String GENDER = "Gender";
    private static final String AGE = "age";
    private static final String BIRTH_DATE = "birthdate";

    List<Item> arrayOfList;
    ListView listView;
    NewsRowAdapter objAdapter;
    private static final String TAG = "Your Service";

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

        listView = (ListView) findViewById(R.id.listview);
        listView.setOnItemClickListener(this);

        arrayOfList = new ArrayList<Item>();

        if (Utils.isNetworkAvailable(MainActivity.this)) {

             new MyTask().execute(rssFeed);
        } else {
            //showToast("No Network Connection!!!");
        }
    }

    // My AsyncTask start...
    class MyTask extends AsyncTask<String, Void, String> {

        ProgressDialog pDialog;

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Loading...");
            pDialog.setCancelable(false);
            pDialog.show();
        }

        @Override
        protected String doInBackground(String... params) {
            return Utils.getJSONString(params[0]);
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            if (null != pDialog && pDialog.isShowing()) {
                pDialog.dismiss();
            }

            if (null == result || result.length() == 0) {
                showToast("No data found from web!!!");
                MainActivity.this.finish();
            } else {

                try {
                    JSONObject mainJson = new JSONObject(result);
                    JSONArray jsonArray = mainJson.getJSONArray(ARRAY_NAME);
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject objJson = jsonArray.getJSONObject(i);

                        Item objItem = new Item();

                        objItem.setId(objJson.getInt(ID));
                        objItem.setName(objJson.getString(NAME));
                        objItem.setCity(objJson.getString(CITY));
                        objItem.setGender(objJson.getString(GENDER));
                        objItem.setAge(objJson.getInt(AGE));
                        objItem.setBirthdate(objJson.getString(BIRTH_DATE));

                        arrayOfList.add(objItem);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }

                Collections.sort(arrayOfList, new Comparator<Item>() {

                    @Override
                    public int compare(Item lhs, Item rhs) {
                        return (lhs.getAge() - rhs.getAge());
                    }
                });
                setAdapterToListview();
            }
        }
    }

    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        showDeleteDialog(position);
    }

    private void showDeleteDialog(final int position) {
        AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this)
                .create();
        alertDialog.setTitle("Delete ??");
        alertDialog.setMessage("Are you sure want to Delete it??");
        alertDialog.setButton("No", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                dialog.dismiss();
            }
        });
        alertDialog.setButton2("Yes", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {
                arrayOfList.remove(position);
                objAdapter.notifyDataSetChanged();
            }
        });
        alertDialog.show();
    }

    public void setAdapterToListview() {
        objAdapter = new NewsRowAdapter(MainActivity.this, R.layout.row,
                arrayOfList);
        listView.setAdapter(objAdapter);
    }

    public void showToast(String msg) {
        Toast.makeText(MainActivity.this, msg, Toast.LENGTH_LONG).show();
    }
}
4

2 に答える 2

0

アラーム マネージャを使用して、特定の時間間隔で Web サービスを呼び出すことができます。ここでアラームマネージャーを使用して確認できます

于 2013-07-16T04:32:14.630 に答える