我在JSONArray中有两个图像。首先是图像,其他是视频的缩略图。缩略图显示但图像未显示。我无法理解问题是什么。我很乐意帮助你正确地获取json。
public class MainActivity extends AppCompatActivity {
private GridView mGridView;
private GridItem newItem;
private GridViewAdapter mGridAdapter;
private ArrayList<GridItem> mGridData;
public static final String KEY_USERID = "user_id";
private static final String REGISTER_URL = "http://xxx.xxx.xxx.xxx:8001/api/userlog/fetch/";
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridView = (GridView)findViewById(R.id.gridView);
registerUser();
mGridData = new ArrayList<>();
mGridAdapter = new GridViewAdapter(this, R.layout.profile_gridview_item, mGridData);
mGridView.setAdapter(mGridAdapter);
}
///////////////////////////////////////// Volley Method Starts //////////////////////////////////////////////////////////////////
private void registerUser(){
StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONObject jObj = new JSONObject(response);
System.out.println(jObj);
String status = jObj.getString("status");
// Now check status value
if (status.equals("0")) {
Toast.makeText(getApplicationContext(), "There was some error! Please try again.", Toast.LENGTH_LONG).show();
}else if(status.equals("1")){
JSONArray result = jObj.getJSONArray("results");
for(int i = 0 ; i <result.length() ; i++){
JSONObject json_data = result.getJSONObject(i);
newItem = new GridItem();
String image = json_data.getString("log_file");
String date = json_data.getString("create_day");
String month = json_data.getString("create_month");
newItem.setImage(image);
newItem.setDate(date);
newItem.setMonth(month);
mGridData.add(newItem);
}
mGridAdapter.setGridData(mGridData);
} else {
// Error in login. Get the error message
String errorMsg = jObj.getString("error_msg");
Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
// JSON error
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_LONG).show();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put(KEY_USERID, "1");
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
requestQueue.add(stringRequest);
}
///////////////////////////////////////// Volley Method Ends ////////////////////////////////////////////////////////////////////
}