У меня есть класс, который возвращает реестры моей базы данных посредством Веба service, и в response я призываю к методу, что меня создает LinearLayout с информацией, вернувшей моим webservice, но volley он говорит мне, что requestQueue.add (jsonObjectRequest), он null, это мой Веб service
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);
notices.setNombre(jsonObject.optString("nombre"));
notices.setTitulo(jsonObject.optString("titulo"));
notices.setFecha(jsonObject.optString("fecha"));
notices.setTexto(jsonObject.optString("texto"));
notices.setUrl1(jsonObject.optString("url1"));
notices.setUrl2(jsonObject.optString("url2"));
notices.setUrl3(jsonObject.optString("url3"));
notices.setUrl4(jsonObject.optString("url4"));
notices.setUrl5(jsonObject.optString("url5"));
notices.setUrl6(jsonObject.optString("url6"));
listNotices.add(notices);
}
progressDialog.hide();
addObject(listNotices);
} 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 addObject(ArrayList datos) {
List<Notices> list = datos;
LayoutInflater inflater = LayoutInflater.from(getContext());
LinearLayout linearLayout = (LinearLayout) inflater.inflate(R.layout.card, null, false);
nombre = linearLayout.findViewById(R.id.nombre_opt_notices);
fecha = linearLayout.findViewById(R.id.fecha_opt_notices);
titulo = linearLayout.findViewById(R.id.titulo_opt_notices);
texto = linearLayout.findViewById(R.id.texto_opt_notices);
nombre.setText(list.get(0).getNombre());
fecha.setText(list.get(1).getFecha());
titulo.setText(list.get(2).getTitulo());
texto.setText(list.get(3).getTexto());
layout.addView(linearLayout);
}
и мой файл php, что является тем, который реализует связь, этот:
<?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";
$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);
?>
Конечно ты считаешь определенной переменную:
private RequestQueue requestQueue;
, но ты не инициализировал "queue" запросов, должен реализовывать это после того, как инициализируешь твой Activity, Adapter, Услугу. Для этого необходим контекст:
// Create a Queue of requests
requestQueue = Volley.newRequestQueue(context);