Good, I am developing an Android application that I always compiled on a cell phone with API 21, and now that I am testing with other devices, I can not make it run on an API 16, when the minimum API to run the application is API fifteen. The code in which I am having an error is the following:
public class JSONTaskLoguin extends AsyncTask<String, String, String>{
private ProgressDialog dialog;
public JSONTaskLoguin(ActLoguin actividad){
dialog = new ProgressDialog(actividad);
}
@Override
protected void onPreExecute() {
try{
dialog.setMessage("Iniciando sesión, espere.");
dialog.show();
}
catch (Exception e){
}
}
@Override
protected String doInBackground(String... Parametros) {
HttpURLConnection conexion = null;
BufferedReader reader = null;
try {
URL url = new URL(Parametros[0]);
conexion = (HttpURLConnection) url.openConnection();
conexion.connect();
InputStream stream = conexion.getInputStream();
reader = new BufferedReader(new InputStreamReader(stream));
StringBuffer buffer = new StringBuffer();
// Lee línea por línea lo que se devuelve del WebService.
String Line = "";
while ((Line = reader.readLine()) != null) {
buffer.append(Line);
}
return buffer.toString(); // retorna Datos Manipulables en onPostExecute
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (conexion != null) {
conexion.disconnect();
}
try {
if (reader != null) {
reader.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
try {
Thread.sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
// Método una vez que lleguen los datos ...
@Override
protected void onPostExecute(String Resultado) {
// se obtienen los datos del Resultado.
//Toast.makeText(BusquedaContratistas.this,Resultado,Toast.LENGTH_LONG).show();
super.onPostExecute(Resultado);
String usuario="",contra="";
Boolean error=true;
try {
JSONArray ResultadosEnArray = new JSONArray(Resultado);
for (int i = 0; i < ResultadosEnArray.length(); i++) {
JSONObject Objeto = ResultadosEnArray.getJSONObject(i);
Log.d("Salida idUsuario",Objeto.getString("idUsuario"));
Log.d("Salida Nombre:", Objeto.getString("Nombre"));
Log.d("Salida apellidoPaterno:", Objeto.getString("apellidoPaterno"));
Log.d("Salida apellidoMaterno:", Objeto.getString("apellidoMaterno"));
Log.d("Salida correoE:", Objeto.getString("correoElectronico"));
Log.d("Salida telefono:", Objeto.getString("telefono"));
Log.d("Salida nombreUsuario:", Objeto.getString("nombreUsuario"));
Log.d("Salida contraseña:", Objeto.getString("contraseña"));
if(txtUsuario.getText().toString().trim().equals(Objeto.getString("nombreUsuario"))){
if(txtContraseña.getText().toString().trim().equals(Objeto.getString("contraseña"))){
Intent I=new Intent(ActLoguin.this,ActPrincipal.class);
Toast.makeText(ActLoguin.this,"Bienvenido "+Objeto.getString("nombreUsuario"),Toast.LENGTH_LONG).show();
usuario=Objeto.getString("nombreUsuario");
contra=Objeto.getString("contraseña");
error=false;
I.putExtra("Nombre",Objeto.getString("nombreUsuario"));
I.putExtra("Correo",Objeto.getString("correoElectronico"));
if(Objeto.getString("TipoUsuario").equals("admin")){
SharedPreferences prefs =
getSharedPreferences("MisPreferencias",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("idUsuario", Objeto.getString("idUsuario").trim());
editor.putString("Usuario", txtUsuario.getText().toString().trim());
editor.putString("Contraseña", txtContraseña.getText().toString().trim());
editor.putString("Correo",Objeto.getString("correoElectronico"));
editor.commit();
Intent In=new Intent(ActLoguin.this,ActPrincipalAdmin.class);
Toast.makeText(ActLoguin.this,"Bienvenido "+Objeto.getString("nombreUsuario"),Toast.LENGTH_LONG).show();
In.putExtra("Nombre",Objeto.getString("nombreUsuario"));
In.putExtra("Correo",Objeto.getString("correoElectronico"));
startActivity(In);
finish();
}
else{
SharedPreferences prefs =
getSharedPreferences("MisPreferencias",
Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putString("idUsuario", Objeto.getString("idUsuario").trim());
editor.putString("Usuario", txtUsuario.getText().toString().trim());
editor.putString("Contraseña", txtContraseña.getText().toString().trim());
editor.commit();
startActivity(I);
finish();
}
}
else{
}
}
else{
}
Log.d("Valores",usuario+" "+contra);
Log.d("error",error.toString());
}
} catch (Throwable t) {
Log.e("Falla", t.toString());
}
if (dialog.isShowing()) {
dialog.dismiss();
}
if(error){
Toast.makeText(ActLoguin.this,"Error en tus campos, revisalos e inténtalo de nuevo. ",Toast.LENGTH_LONG).show();
txtContraseña.setError("El campo no coincide con la base de datos");
txtUsuario.setError("El campo no coincide con la base de datos");
}
else {
}
}
}
That's the code I use to retrieve the data from a webService and pars them for later use (it's a WebService with which I perform a loguin). In the API 22 phone the code works without problems, but in API 16 the logcat shows me this: The most logical thing to do would be to think that the URL that I send is wrong, but if they check The link that shows the logcat if it retrieves the data, but throws error , just like that.
According to Logcat, the error is in this line:
InputStream stream = conexion.getInputStream();
Any idea why this happens?