2017-07-04 81 views
-23

我想從Android獲取數據到自定義ListView,但我無法獲取!請幫助從jsonArray請求沒有響應

MY JSON:_

{ 
    "College":[ 
     { 
     "Name":"NITK", 
     "Logo":"http:\/\/192.168.43.164\\Webapp\\Collegelogos\\nitk.jpg" 
     }, 
     { 
     "Name":"IITR", 
     "Logo":"http:\/\/192.168.43.164\\Webapp\\Collegelogos\\iitr.jpg" 
     }, 
     { 
     "Name":"NITT", 
     "Logo":"http:\/\/192.168.43.164\\Webapp\\Collegelogos\\nitt.jpg" 
     }, 
     { 
     "Name":"IITB", 
     "Logo":"http:\/\/192.168.43.164\\Webapp\\Collegelogos\\iitb.png" 
     }, 
     { 
     "Name":"IITG", 
     "Logo":"http:\/\/192.168.43.164\\Webapp\\Collegelogos\\iitg.png" 
     }, 
     { 
     "Name":"IITD", 
     "Logo":"http:\/\/192.168.43.164\\Webapp\\Collegelogos\\iitd.png" 
     }, 
     { 
     "Name":"BITS", 
     "Logo":"http:\/\/192.168.43.164\\Webapp\\Collegelogos\\bits.png" 
     } 
    ] 
} 

CODE: -

public class MainActivity extends AppCompatActivity { 
    private List<College> listcollege; 

    //Creating Views 
    private RecyclerView recyclerView; 
    private RecyclerView.LayoutManager layoutManager; 
    private RecyclerView.Adapter adapter; 

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

     //Initializing Views 
     recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
     recyclerView.setHasFixedSize(true); 
     layoutManager = new LinearLayoutManager(this); 
     recyclerView.setLayoutManager(layoutManager); 

     listcollege = new ArrayList<>(); 
     getData(); 

    } 

    private void getData(){ 
     //Showing a progress dialog 
     final ProgressDialog loading = ProgressDialog.show(this,"Loading Data", "Please wait...",false,false); 

     //Creating a json array request 
     JsonArrayRequest jsonArrayRequest = new JsonArrayRequest(Config.DATA_URL, 
       new Response.Listener<JSONArray>() { 
        @Override 
        public void onResponse(JSONArray response) { 
         //Dismissing progress dialog 
         Toast.makeText(MainActivity.this, "sachin", Toast.LENGTH_SHORT).show(); 
         loading.dismiss(); 

         //calling method to parse json array 
         parseData(response); 
        } 
       }, 
       new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

        } 
       }); 

     //Creating request queue 
     RequestQueue requestQueue = Volley.newRequestQueue(this); 

     //Adding request to the queue 
     requestQueue.add(jsonArrayRequest); 
    } 
    public void parseData(JSONArray array) { 
     for (int i = 0; i < array.length(); i++) { 
      College college = new College(); 
      JSONObject json = null; 
      try { 
       json = array.getJSONObject(i); 
       college.setImageUrl(json.getString(Config.TAG_IMAGE_URL)); 
       String s = (json.getString(Config.TAG_NAME)); 
       Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); 
       college.setName(s); 

      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      listcollege.add(college); 

     } 
      adapter = new CardAdapter(this, listcollege); 
      recyclerView.setAdapter(adapter); 
     // adapter.notifyDataSetChanged(); 

    } 


} 

LOG: - ANDROID MONITOR

07-04 13:02:42.289 16225-16256/com.example.sachin.volleyrecycle I/OpenGLRenderer: Initialized EGL, version 1.4 
07-04 13:02:42.289 16225-16256/com.example.sachin.volleyrecycle D/OpenGLRenderer: Swap behavior 1 
07-04 13:02:42.289 16225-16256/com.example.sachin.volleyrecycle W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 
07-04 13:02:42.289 16225-16256/com.example.sachin.volleyrecycle D/OpenGLRenderer: Swap behavior 0 
07-04 13:02:42.702 16225-16225/com.example.sachin.volleyrecycle E/RecyclerView: No adapter attached; skipping layout 
07-04 13:02:42.946 16225-16225/com.example.sachin.volleyrecycle W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 
+0

我想你還沒有創建一個適當的適配器來顯示你的清單..請檢查你的適配器類。 –

+0

那不是問題!問題我們與數據的獲取 –

+0

[在什麼情況下我可以添加「緊急」或其他類似的短語到我的問題,以獲得更快的答案?](https://meta.stackoverflow.com/questions/326569/under -what-c​​ircumstances-may-i-add-urgent-or-other-similar-phrases-to-my-quest) –

回答

2

1.初始化您adapter並將其設置爲RecyclerViewonCreate()方法。

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

    //Initializing Views 
    recyclerView = (RecyclerView) findViewById(R.id.recyclerView); 
    recyclerView.setHasFixedSize(true); 
    layoutManager = new LinearLayoutManager(this); 
    recyclerView.setLayoutManager(layoutManager); 

    listcollege = new ArrayList<>(); 
    adapter = new CardAdapter(this, listcollege); 
    recyclerView.setAdapter(adapter); 

    getData(); 
} 

2.代替JsonArrayRequest,請嘗試使用JsonObjectRequest

//Creating a json object request 
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest (Config.DATA_URL, 
     new Response.Listener<JSONObject>() { 
      @Override 
      public void onResponse(JSONObject response) { 
       //Dismissing progress dialog 
       Toast.makeText(MainActivity.this, "sachin", Toast.LENGTH_SHORT).show(); 
       loading.dismiss(); 

       // College 
       JSONArray jsonArrayCollege = response.getJSONArray("College"); 

       // calling method to parse json array 
       parseData(jsonArrayCollege); 
      } 
     }, 
     new Response.ErrorListener() { 
      @Override 
      public void onErrorResponse(VolleyError error) { 

      } 
     }); 

//Creating request queue 
RequestQueue requestQueue = Volley.newRequestQueue(this); 

//Adding request to the queue 
requestQueue.add(jsonObjectRequest); 

更新如下parseData()方法:

public void parseData(JSONArray array) { 
    for (int i = 0; i < array.length(); i++) { 
     College college = new College(); 
     JSONObject json = null; 
     try { 
      json = array.getJSONObject(i); 
      college.setImageUrl(json.getString(Config.TAG_IMAGE_URL)); 
      String s = (json.getString(Config.TAG_NAME)); 
      Toast.makeText(this, s, Toast.LENGTH_SHORT).show(); 
      college.setName(s); 

     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     listcollege.add(college); 
    } 
    adapter.notifyDataSetChanged(); 
} 

希望這將有助於〜