0

loopj AsyncHttpClient を使用して、JSON ファイルを解析する簡単な例を探していました。しかし、これまでのところ、有用な情報は見つかりませんでした。:(

解析する単純な JSON ファイル:

{
    "contact": [
        {
                "id": "10",
                "name": "Tom",
                "email": "tom@gmail.com"

                }
        }
  ]
}

私はどんな提案にも感謝します。ありがとう!!

4

3 に答える 3

0
public class MainActivity extends Activity {

    private ProgressDialog pdialog;

    private static String url = "http://highspecificationservers.com/apk/webservice.php";

    private static final String TAG_STUDENT = "student";
    private static final String TAG_FNAME = "fname";
    private static final String TAG_EMAIL = "email";
    private static final String TAG_MOBILE = "mobile";

    JSONArray student = null;

    ArrayList<HashMap<String, String>> studentlist;

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

        studentlist = new ArrayList<HashMap<String, String>>();

        ListView lv = getListView();

        lv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // TODO Auto-generated method stub

                String fname = ((TextView) view.findViewById(R.id.fname))
                        .getText().toString();
                String cost = ((TextView) view.findViewById(R.id.email))
                        .getText().toString();
                String description = ((TextView) view.findViewById(R.id.mobile))
                        .getText().toString();

                Intent in = new Intent(getApplicationContext(),
                        SingleContactActivity.class);
                in.putExtra(TAG_FNAME, fname);
                in.putExtra(TAG_EMAIL, cost);
                in.putExtra(TAG_MOBILE, description);
                startActivity(in);

            }
        });

        new GetStudent().execute();

    }

    private class GetStudent extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pdialog = new ProgressDialog(MainActivity.this);
            pdialog.setMessage("please wait");
            pdialog.setCancelable(false);
            pdialog.show();
        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub

            ServiceHandler sh = new ServiceHandler();

            String jString = sh.makeServiceCall(url, ServiceHandler.GET);

            Log.d("Response:", "> " + jString);

            if (jString != null) {
                try {
                    JSONObject Jsonobj = new JSONObject(jString);

                    student = Jsonobj.getJSONArray(TAG_STUDENT);

                    for (int i = 0; i < student.length(); i++) {
                        JSONObject c = student.getJSONObject(i);
                        String fname = c.getString(TAG_FNAME);
                        String email = c.getString(TAG_EMAIL);
                        String mobile = c.getString(TAG_MOBILE);

                        HashMap<String, String> student = new HashMap<String, String>();

                        student.put(TAG_FNAME, fname);
                        student.put(TAG_EMAIL, email);
                        student.put(TAG_MOBILE, mobile);

                        studentlist.add(student);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if (pdialog.isShowing())
                pdialog.dismiss();
            ListAdapter adapter = new SimpleAdapter(MainActivity.this,
                    studentlist, R.layout.list_item, new String[] { TAG_FNAME,
                            TAG_EMAIL, TAG_MOBILE }, new int[] { R.id.fname,
                            R.id.email, R.id.mobile });

            setListAdapter(adapter);

        }

        private void setListAdapter(ListAdapter adapter) {
            // TODO Auto-generated method stub

        }

    }

    private ListView getListView() {
        // TODO Auto-generated method stub
        return null;
    }

}
于 2014-09-17T08:42:09.233 に答える