2014-04-10 2 views
0

loopj AsyncHttpClient를 사용하여 JSON 파일을 구문 분석하는 간단한 예제를 찾고있었습니다. 그러나 지금까지 나는 유용한 정보를 찾을 수 없었다. :(Android loopj AsyncHttpClient, JSON 구문 분석 예제

간단한 JSON 파일을 구문 분석 :

{ 
    "contact": [ 
     { 
       "id": "10", 
       "name": "Tom", 
       "email": "[email protected]" 

       } 
     } 
    ] 
} 

내가 어떤 제안에 대한 감사 드리겠습니다 감사합니다!이

당신은 먼저 JSON을 얻을 필요가

답변

-1

이동 ... 루프

// 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"); 
      } 
+0

사용하면 여러 연락처가있는 경우 ... – Akshay

+0

-1 그냥 게시 ... 좋지 온라인 자습서 코드로 인기의 복사되어 다른 사람의 코드 oryginal을 가리 키지 않고 우리가 도둑질이라고 부름 ** – Selvin

+2

그러나 Questioner 요구 사항 버디를 수행합니다. – Akshay

2

난 당신이

것을 수행했다고 가정합니다..
{ // 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"); 
,369 이와
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; 
    } 

} 
+0

관련 코드 만 게시 할 수 있습니까? 내가 제대로 이해했다면 많은 불필요한 코드를 포함 시켰습니다. 요청자가 요청한대로 'loopj AsyncHttpClient'를 사용할 수 없습니다. 이것을 달성하는 또 다른 방법을 제안하는 경우, 코드의 설명과 함께 기술하십시오. 코멘트와 설명이있는 관련 코드 만 있다면 답이 훨씬 유용 할 것입니다 (질문자와 미래 독자들에게). 이에 따라 답변을 업데이트하십시오. – kkuilla