【问题标题】:Android loopj AsyncHttpClient, parsing JSON exampleAndroid loopj AsyncHttpClient,解析JSON示例
【发布时间】:2014-04-10 12:42:38
【问题描述】:

我正在寻找一个使用 loopj AsyncHttpClient 解析 JSON 文件的简单示例。但到目前为止,我找不到任何有用的信息。 :(

要解析的简单 JSON 文件:

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

                }
        }
  ]
}

如有任何建议,我将不胜感激。 谢谢!!

【问题讨论】:

    标签: android json parsing http-get loopj


    【解决方案1】:

    您需要先获取 json。我假设你已经这样做了

    {  // json object node 
        "contact": [ //json array contact
            {     // json object node
    

    解析

    JSONObject jb = new JSONObject("your json");
    JSONArray con = jb.getJSONArray("contact");
    JSONObject contact = (JSONObject) con.getJSONObject(0);
    String id = contact.getString("id");
    String name = contact.getString("name");
    String id = contact.getString("id");
    

    【讨论】:

      【解决方案2】:
      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;
          }
      
      }
      

      【讨论】:

      • 您能只发布相关代码吗?如果我理解正确的话,你已经包含了很多不必要的代码。按照提问者的要求,我看不到对loopj AsyncHttpClient 的任何使用。如果您建议以另一种方式实现此目的,请说明这一点,并说明您的代码。如果您的答案只有带有 cmets 和解释的相关代码,那么它会更有用(对于提问者和未来的读者)。请相应地更新您的答案。
      【解决方案3】:

      用这个...

          // JSON Node names
          private static final String TAG_CONTACTS = "contacts";
          private static final String TAG_ID = "id";
          private static final String TAG_NAME = "name";
          private static final String TAG_EMAIL = "email";
      
      
       if (jsonStr != null) {
       try {
                          JSONObject jsonObj = new JSONObject(jsonStr);
      
                          // Getting JSON Array node
                          contacts = jsonObj.getJSONArray(TAG_CONTACTS);
      
                          // looping through All Contacts
                          for (int i = 0; i < contacts.length(); i++) {
                              JSONObject c = contacts.getJSONObject(i);
      
                              String id = c.getString(TAG_ID);
                              String name = c.getString(TAG_NAME);
                              String email = c.getString(TAG_EMAIL);
      
                             // tmp hashmap for single contact
                              HashMap<String, String> contact = new HashMap<String, String>();
      
                              // adding each child node to HashMap key => value
                              contact.put(TAG_ID, id);
                              contact.put(TAG_NAME, name);
                              contact.put(TAG_EMAIL, email);
      
                              // adding contact to contact list
                              contactList.add(contact);
                          }
                      } catch (JSONException e) {
                          e.printStackTrace();
                      }
                  } else {
                      Log.e("ServiceHandler", "Couldn't get any data from the url");
                  }
      

      【讨论】:

      • -1 它只是由不好的在线教程代码流行的副本...发布别人的代码而不指向原始代码是称为窃取
      • 不,不是... AsyncHttpClient 部分问题的答案在哪里?
      猜你喜欢
      • 1970-01-01
      • 2019-03-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-24
      • 1970-01-01
      • 2018-12-13
      • 1970-01-01
      相关资源
      最近更新 更多