Java 没有来自jsonArray请求的响应
我正在尝试从Android获取数据到自定义ListView,但无法获取!请帮忙 我的JSON:_Java 没有来自jsonArray请求的响应,java,android,json,Java,Android,Json,我正在尝试从Android获取数据到自定义ListView,但无法获取!请帮忙 我的JSON:_ { "College":[ { "Name":"NITK", "Logo":"http:\/\/192.168.43.164\\Webapp\\Collegelogos\\nitk.jpg" }, { "Name":"IITR", "Logo":"http:\/\/192
{
"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"
}
]
}
代码:-
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();
}
}
1。从
onCreate()
方法初始化适配器
并将其设置为RecyclerView
@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();
}
3.更新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();
}
public void parseData(JSONArray数组){
对于(int i=0;i
希望这会有所帮助~我认为您没有创建合适的适配器来显示列表。请检查适配器类。这不是问题所在!我可以给你一个主意:如果你想快速解决一些问题,你必须为此付费。雇佣一个开发者来解决你的问题。如果你不想——改进你的问题,这样就更容易理解问题所在,并保持耐心。
//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);
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();
}