He estado mirando por el foro respuestas sobre este mismo error y dicen que es porque volley devuelve los datos como vacío, tengo una aplicación que imita una seccion de noticias similar a la de facebook y al publicar recibe maximo 6 imagenes de las cuales solo guardo en la base de datos la url para posteriormente cargarlas con glide creando objetos dinamicamente para las publicaciones ya realizadas y traer la informacion desde el servidor, este error se me presenta cuando intento cargar la seccion de las noticias despues de haber publicado cuatro imagenes o mas pero si son 3 imagenes o menos la seccion de noticias carga muy bien, esto sucede desde un celular fisico en este caso "ZTE BLade A521" pero desde el emulador de android studio no me da este error, si borro el registro en la base de datos que he realizado anteriormente desde el cel en fisico vuelve a cargar todos los datos, al verificar si algun dato no se envio me doy cuenta que guarda todo inclusive las urls de las imagenes, pero volley me indica el mismo error a menos que borre el registro, en mi seccion cuando quiero mostrar las publicaciones cargadas por mas que que recargue me sigue saliendo ese mismo error. de ante mano muchisimas gracias
Esta es mi clase que muestra las publicaciones realizadas:
public class SecondOptionNotices extends Fragment implements Response.Listener<JSONObject>, Response.ErrorListener{
View view;
ViewGroup layout;
private ProgressDialog progressDialog;
LinearLayout contentLayouts;
LinearLayout linearLayout;
LayoutInflater inflater;
SwipeRefreshLayout swipeRefreshLayout;
ScrollView scrollViewNotices;
LinearLayout loadBottom;
RequestQueue requestQueue;
JsonObjectRequest jsonObjectRequest;
TextView nombre, fecha, titulo, texto;
ImageView img1, img2, img3, img4, img5, img6;
FloatingActionButton btnFloatingPost;
int checkBottom = 0;
public interface UpdateNotices{
public void updateViews();
}
UpdateNotices updateViewNotices;
public static SecondOptionNotices newInstance(){
return new SecondOptionNotices();
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
try {
updateViewNotices = (UpdateNotices) context;
}catch (ClassCastException e){
throw new ClassCastException(context.toString() + " Debe implementar la interfaz Update en su Activity");
}
}
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.second_option_notices, container, false);
return view;
}
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
init();
}
private void init() {
inflater = LayoutInflater.from(getContext());
contentLayouts = view.findViewById(R.id.content_layouts);
swipeRefreshLayout = view.findViewById(R.id.swipe_notices);
scrollViewNotices = view.findViewById(R.id.scrollview_notices);
requestQueue = Volley.newRequestQueue(Objects.requireNonNull(getContext()));
btnFloatingPost = view.findViewById(R.id.btn_floating_notices);
addLayouts();
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
@Override
public void onRefresh() {
deleteNoticesRefresh();
loadWebService();
swipeRefreshLayout.setRefreshing(false);
}
});
btnFloatingPost.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateViewNotices.updateViews();
}
});
scrollViewNotices.getViewTreeObserver()
.addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
@Override
public void onScrollChanged() {
if(scrollViewNotices.getChildAt(0).getBottom() <= (scrollViewNotices.getHeight() + scrollViewNotices.getScrollY())){
addLayouts();
}
}
});
}
private void deleteNoticesRefresh(){
if(layout.getChildCount() > 0){
layout.removeAllViews();
}
}
private void loadWebService() {
progressDialog = new ProgressDialog(getContext());
progressDialog.setMessage("Cargando...");
progressDialog.show();
String ip = getString(R.string.ip);
String url = ip + "/firstTestSmb/wsConsultarList.php";
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null, this, this);
try{
requestQueue.add(jsonObjectRequest);
}catch (NullPointerException n){
n.printStackTrace();
Log.i("ERROR_NULL", n.toString());
}
}
@Override
public void onResponse(JSONObject response) {
//Notices notices = null;
JSONArray jsonArray = response.optJSONArray("noticias");
try {
for(int i = 0; i < jsonArray.length(); i++){
//notices = new Notices();
JSONObject jsonObject = null;
jsonObject = jsonArray.getJSONObject(i);
String datos[] = {jsonObject.optString("nombre"),
jsonObject.optString("titulo"),
jsonObject.optString("fecha"),
jsonObject.optString("texto"),
jsonObject.optString("url1"),
jsonObject.optString("url2"),
jsonObject.optString("url3"),
jsonObject.optString("url4"),
jsonObject.optString("url5"),
jsonObject.optString("url6")};
addObject(datos);
}
progressDialog.hide();
} catch (JSONException e) {
progressDialog.hide();
e.printStackTrace();
Toast.makeText(getContext(), "No se ha podido establecer conexion con el servidor" + " " + response, Toast.LENGTH_SHORT).show();
Log.i("ERROR_ARRAY", response.toString());
}
}
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.hide();
Toast.makeText(getContext(), "No se Ha Podido Conectar", Toast.LENGTH_SHORT).show();
Log.i("ERROR_CONEXION", error.toString());
}
private void addLayouts(){
checkBottom = 1;
linearLayout = (LinearLayout) inflater.inflate(R.layout.layouts_notices, null, false);
contentLayouts.addView(linearLayout);
loadBottom = linearLayout.findViewById(R.id.load_bottom);
loadBottom.setVisibility(View.VISIBLE);
layout = linearLayout.findViewById(R.id.contentObj);
loadWebService();
loadBottom.setVisibility(View.GONE);
}
private void addObject(String datos[]) {
String urlsImages[] = {
datos[4],
datos[5],
datos[6],
datos[7],
datos[8],
datos[9]
};
if(urlsImages[0].equalsIgnoreCase("No Registra") &&
urlsImages[1].equalsIgnoreCase("No Registra") &&
urlsImages[2].equalsIgnoreCase("No Registra") &&
urlsImages[3].equalsIgnoreCase("No Registra") &&
urlsImages[4].equalsIgnoreCase("No Registra") &&
urlsImages[5].equalsIgnoreCase("No Registra")){
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_dates, null, false);
nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
texto = relativeLayout.findViewById(R.id.texto_opt_notices);
nombre.setText(datos[0]);
fecha.setText(datos[1]);
titulo.setText(datos[2]);
texto.setText(datos[3]);
layout.addView(relativeLayout);
}else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
urlsImages[1].equalsIgnoreCase("No Registra") &&
urlsImages[2].equalsIgnoreCase("No Registra") &&
urlsImages[3].equalsIgnoreCase("No Registra") &&
urlsImages[4].equalsIgnoreCase("No Registra") &&
urlsImages[5].equalsIgnoreCase("No Registra")){
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_one_image, null, false);
nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
texto = relativeLayout.findViewById(R.id.texto_opt_notices);
img1 = relativeLayout.findViewById(R.id.image_alone_card);
nombre.setText(datos[0]);
fecha.setText(datos[1]);
titulo.setText(datos[2]);
texto.setText(datos[3]);
loadImages(0, urlsImages);
layout.addView(relativeLayout);
}else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
!urlsImages[1].equalsIgnoreCase("No Registra") &&
urlsImages[2].equalsIgnoreCase("No Registra") &&
urlsImages[3].equalsIgnoreCase("No Registra") &&
urlsImages[4].equalsIgnoreCase("No Registra") &&
urlsImages[5].equalsIgnoreCase("No Registra")){
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_two_image, null, false);
nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
texto = relativeLayout.findViewById(R.id.texto_opt_notices);
img1 = relativeLayout.findViewById(R.id.image_alone_card);
img2 = relativeLayout.findViewById(R.id.image_two_card);
nombre.setText(datos[0]);
fecha.setText(datos[1]);
titulo.setText(datos[2]);
texto.setText(datos[3]);
loadImages(1, urlsImages);
layout.addView(relativeLayout);
}else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
!urlsImages[1].equalsIgnoreCase("No Registra") &&
!urlsImages[2].equalsIgnoreCase("No Registra") &&
urlsImages[3].equalsIgnoreCase("No Registra") &&
urlsImages[4].equalsIgnoreCase("No Registra") &&
urlsImages[5].equalsIgnoreCase("No Registra")){
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_tree_image, null, false);
nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
texto = relativeLayout.findViewById(R.id.texto_opt_notices);
img1 = relativeLayout.findViewById(R.id.image_alone_card);
img2 = relativeLayout.findViewById(R.id.image_two_card);
img3 = relativeLayout.findViewById(R.id.image_tree_card);
nombre.setText(datos[0]);
fecha.setText(datos[1]);
titulo.setText(datos[2]);
texto.setText(datos[3]);
loadImages(2, urlsImages);
layout.addView(relativeLayout);
}else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
!urlsImages[1].equalsIgnoreCase("No Registra") &&
!urlsImages[2].equalsIgnoreCase("No Registra") &&
!urlsImages[3].equalsIgnoreCase("No Registra") &&
urlsImages[4].equalsIgnoreCase("No Registra") &&
urlsImages[5].equalsIgnoreCase("No Registra")){
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_four_image, null, false);
nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
texto = relativeLayout.findViewById(R.id.texto_opt_notices);
img1 = relativeLayout.findViewById(R.id.image_alone_card);
img2 = relativeLayout.findViewById(R.id.image_two_card);
img3 = relativeLayout.findViewById(R.id.image_tree_card);
img4 = relativeLayout.findViewById(R.id.image_four_card);
nombre.setText(datos[0]);
fecha.setText(datos[1]);
titulo.setText(datos[2]);
texto.setText(datos[3]);
loadImages(3, urlsImages);
layout.addView(relativeLayout);
}else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
!urlsImages[1].equalsIgnoreCase("No Registra") &&
!urlsImages[2].equalsIgnoreCase("No Registra") &&
!urlsImages[3].equalsIgnoreCase("No Registra") &&
!urlsImages[4].equalsIgnoreCase("No Registra") &&
urlsImages[5].equalsIgnoreCase("No Registra")){
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_five_image, null, false);
nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
texto = relativeLayout.findViewById(R.id.texto_opt_notices);
img1 = relativeLayout.findViewById(R.id.image_alone_card);
img2 = relativeLayout.findViewById(R.id.image_two_card);
img3 = relativeLayout.findViewById(R.id.image_tree_card);
img4 = relativeLayout.findViewById(R.id.image_four_card);
img5 = relativeLayout.findViewById(R.id.image_five_card);
nombre.setText(datos[0]);
fecha.setText(datos[1]);
titulo.setText(datos[2]);
texto.setText(datos[3]);
loadImages(4, urlsImages);
layout.addView(relativeLayout);
}else if(!urlsImages[0].equalsIgnoreCase("No Registra") &&
!urlsImages[1].equalsIgnoreCase("No Registra") &&
!urlsImages[2].equalsIgnoreCase("No Registra") &&
!urlsImages[3].equalsIgnoreCase("No Registra") &&
!urlsImages[4].equalsIgnoreCase("No Registra") &&
!urlsImages[5].equalsIgnoreCase("No Registra")){
RelativeLayout relativeLayout = (RelativeLayout) inflater.inflate(R.layout.card_six_image, null, false);
nombre = relativeLayout.findViewById(R.id.nombre_opt_notices);
fecha = relativeLayout.findViewById(R.id.fecha_opt_notices);
titulo = relativeLayout.findViewById(R.id.titulo_opt_notices);
texto = relativeLayout.findViewById(R.id.texto_opt_notices);
img1 = relativeLayout.findViewById(R.id.image_alone_card);
img2 = relativeLayout.findViewById(R.id.image_two_card);
img3 = relativeLayout.findViewById(R.id.image_tree_card);
img4 = relativeLayout.findViewById(R.id.image_four_card);
img5 = relativeLayout.findViewById(R.id.image_five_card);
img6 = relativeLayout.findViewById(R.id.image_six_card);
nombre.setText(datos[0]);
fecha.setText(datos[1]);
titulo.setText(datos[2]);
texto.setText(datos[3]);
loadImages(5, urlsImages);
layout.addView(relativeLayout);
}
}
private void loadImages(int id, String urls[]){
switch (id){
case 0:
Glide.with(getContext())
.load(urls[0])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img1);
break;
case 1:
Glide.with(getContext())
.load(urls[0])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img1);
Glide.with(getContext())
.load(urls[1])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img2);
break;
case 2:
Glide.with(getContext())
.load(urls[0])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img1);
Glide.with(getContext())
.load(urls[1])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img2);
Glide.with(getContext())
.load(urls[2])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img3);
break;
case 3:
Glide.with(getContext())
.load(urls[0])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img1);
Glide.with(getContext())
.load(urls[1])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img2);
Glide.with(getContext())
.load(urls[2])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img3);
Glide.with(getContext())
.load(urls[3])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img4);
break;
case 4:
Glide.with(getContext())
.load(urls[0])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img1);
Glide.with(getContext())
.load(urls[1])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img2);
Glide.with(getContext())
.load(urls[2])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img3);
Glide.with(getContext())
.load(urls[3])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img4);
Glide.with(getContext())
.load(urls[4])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img5);
break;
case 5:
Glide.with(getContext())
.load(urls[0])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img1);
Glide.with(getContext())
.load(urls[1])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img2);
Glide.with(getContext())
.load(urls[2])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img3);
Glide.with(getContext())
.load(urls[3])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img4);
Glide.with(getContext())
.load(urls[4])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img5);
Glide.with(getContext())
.load(urls[5])
.centerCrop()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC)
.skipMemoryCache(true)
.thumbnail(0.1f)
.into(img6);
break;
}
}
}
Este es mi archivo php que llamo con volley para traer toda la informacion:
<?PHP
$hostname_localhost="localhost";
$database_localhost="db_test_first";
$username_localhost="root";
$password_localhost="";
$json=array();
$conexion = mysqli_connect($hostname_localhost,$username_localhost,$password_localhost,$database_localhost);
$consulta="select * from noticias order by id desc limit 20 offset 0";
$resultado=mysqli_query($conexion,$consulta);
while($registro=mysqli_fetch_array($resultado)){
$result["nombre"]=$registro['nombre'];
$result["titulo"]=$registro['titulo'];
$result["fecha"]=$registro['fecha'];
$result["texto"]=$registro['texto'];
$result["url1"]=$registro['url1'];
$result["url2"]=$registro['url2'];
$result["url3"]=$registro['url3'];
$result["url4"]=$registro['url4'];
$result["url5"]=$registro['url5'];
$result["url6"]=$registro['url6'];
$json['noticias'][]=$result;
//echo $registro['id'].' - '.$registro['nombre'].'<br/>';
}
mysqli_close($conexion);
echo json_encode($json);
?>
En archivo php al intentar abrirlo desde el servidor no me muestra nada todo en blanco
String
, unJSONArray
o unJSONObject
. Según lo que sea, debes saber cómo leerlo y sacar la información que hay dentro. Si nos muestras lo que estás recibiendo en la respuesta te podemos ayudar. Pon esto en elonResponse
y revisa el logcat:Log.d("Volley", response.toString());
– A. Cedano el 22 mar. 19 a las 0:57