2014-11-28 3 views
-1

json으로는 다음과 같습니다안드로이드에서 JSon 데이터를 어떻게 얻을 수 있습니까?

[{"id":"1","name":"Mihai","email":"[email protected]","password":"1234","phone":"765889345"},{"id":"2","name":"Robin","email":"[email protected]","password":"1234","phone":"765453434"}] 

// Json 
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
      .detectAll() 
      .penaltyLog() 
      .penaltyDialog() 
      .build()); 

    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll() 
      .penaltyLog() 
      .build()); 
    StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder() 
      .detectDiskReads() 
      .detectDiskWrites() 
      .detectNetwork() // or .detectAll() for all detectable problems 
      .penaltyLog() 
      .build()); 
    StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder() 
      .detectLeakedSqlLiteObjects() 
      .penaltyLog() 
      .penaltyDeath() 
      .build()); 


    TextView uid = (TextView) findViewById(R.id.titlu_anunt1); 
    TextView name1 = (TextView) findViewById(R.id.descriere_anunt1); 
    TextView email1 = (TextView) findViewById(R.id.telefon_anunt1); 


    JSONObject json = null; 
    String str = ""; 
    HttpResponse response; 
    HttpClient myClient = new DefaultHttpClient(); 
    HttpPost myConnection = new HttpPost("http://appz.esy.es/get_user.php"); 

    try { 
     response = myClient.execute(myConnection); 
     str = EntityUtils.toString(response.getEntity(), "UTF-8"); 

    } 
    catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } 
    catch (IOException e) { 
     e.printStackTrace(); 
    } 


    try { 
     JSONArray jArray = new JSONArray(str); 
     json = jArray.getJSONObject(0); 


     uid.setText(json.getString("id")); 
     name1.setText(json.getString("name")); 
     email1.setText(json.getString("email")); 


    } 
    catch (JSONException e) { 
     e.printStackTrace(); 
    } 

가 어떻게 내 textviews에 모든 개체를 추가 할 수있는 코드? 지금은 첫 번째 텍스트 뷰에서 json의 객체 하나만 표시 할 수 있지만 ID를 증가시키고 싶습니다. id1 정보를 표시하고 싶습니다. 이름, 위치 및 기타 내용을 textviews에 표시하고 싶습니다. 1. 그 후에 id2에 이름과 위치를 textviews에 표시하고 싶습니다. 2.

+1

문제는 무엇 :

방금이를 추가 할 필요가 요청을 만들려면? 너는 json을 가져올 수 없다? 너는 json을 파싱 할 수 없다? 또는 구문 분석 된 json을 사용하여 앱의 뷰를 업데이트 할 수 없습니까? –

+0

'json = sb.toString();'. 승인. 이제 json은 언급 한 json 텍스트를 포함해야합니다. 지금까지는 괜찮습니까? – greenapps

+0

이러한 요청에 대해 Android Volley를 사용해보십시오. JSONObjectRequest가 있습니다. – Robert

답변

0

사용할 수있는 volley 라이브러리를 사용할 수 있으며 데이터를 얻는 방법보다는 앱에 집중할 수 있습니다. 라이브러리를 프로젝트에 this tutorial 다음에 추가하거나 AndroidStudio를 사용하는 경우 그라데에 추가 할 수 있습니다 as seen here.

 //I think a StringRequest is more convenient in your case cause you can 
     //make a new JSONArray straight from the String 
    StringRequest request = new StringRequest(url, new Response.Listener<String>() { 
     @Override 
     public void onResponse(String response) { 
      //Here is where you process your data, you could call a method to parse your data 
      parseData(response); 
     } 
    }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 
      //Here is where you handle the errors 
     } 
    }); 

    RequestQueue queue = Volley.newRequestQueue(Here_goes_a_Context_object); 
    //This will send the request 
    queue.add(request); 

    public void parseData(String response) { 

      try { 

       //Your JSONArray doesn't have a name so you can't find it with the USER_TAG 
       //You need to make your JSONObject a String and then make a new JSONArray with that String 
       user = new JSONArray(response); 

       //The rest of the code looks good to me 
       ... 
      } 
     } 
+0

시도 { \t \t \t \t \t \t 사용자 = 새 JSONArray (json.toString()); \t \t \t JSONObject c = user.getJSONObject (0); \t \t \t // JSON 항목을 변수에 저장 \t \t \t String id = c.getString (TAG_ID); \t \t \t 문자열 이름 = c.getString (TAG_NAME); \t \t \t String email = c.getString (TAG_EMAIL); 너 이런 뜻이야? 하지만 아무 일도 없었어요 – dodo

+0

나는 내 대답을 –

+0

내게 업 그레 나를 업 ...이 때 나는 json을 사용할 때 처음이다. – dodo

관련 문제