Estoy haciendo una peticion a un webService desde un AsynTask() y me está dando el error de que la respuesta es nula.
He probado en el navegador a poner la URL de mi webservice y si me muestra el resultado de la consulta, por tanto el error está en la recepción de la consulta por parte del cliente android.
Os dejo el código:
public void peticionAsync()
{
PeticionAsync pA = new PeticionAsync();
pA.execute(miUrl);
}
class PeticionAsync extends AsyncTask<String, Void, String>
{
ProgressDialog pd;
@Override
protected String doInBackground(String... urls)
{
return POST(miUrl);
}
// onPostExecute displays the results of the AsyncTask.
protected void onPreExecute() {
super.onPreExecute();
Persona p = new Persona();
pd = new ProgressDialog(Listado.this);
pd.setMessage("Conectando con el servidor");
pd.show();
Gson gson = new Gson();
json = gson.toJson(p);
}
@Override
protected void onPostExecute(String result)
{
Log.i("RESULT: ","Consulta:"+result);
if (result.trim().equals("0"))
{
Toast toast1 = Toast.makeText(getApplicationContext(), "Error en la conexión", Toast.LENGTH_LONG);
toast1.show();
Intent passIntent = new Intent();
passIntent.setClass(Listado.this,Menu.class);
startActivity(passIntent);
}
else{
//Parsear result de JSON con GSON
Log.i("JSONresumen",result);
Gson gson = new Gson();
listaPlan = gson.fromJson(result, new TypeToken<List<PlanResumido>>(){}.getType());
if (pd != null)
{
pd.dismiss();
}
actualizarDisplay();
}
}
public String POST(String url) {
HttpURLConnection urlConnection;
URL urlA;
if (android.os.Build.VERSION.SDK_INT > 9) {
StrictMode.ThreadPolicy policy =
new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
}
try {
urlA = new URL("https://safeplanapp.es/SPWS/SPlistadoPlanes.php");
} catch (IOException e) {
throw new RuntimeException(e);
}
String data = json;
String result = null;
try
{
urlConnection = (HttpURLConnection) urlA.openConnection();
urlConnection.setDoOutput(true);
urlConnection.setRequestProperty("Content-Type", "application/json");
urlConnection.setRequestProperty("Accept", "application/json");
urlConnection.setRequestMethod("POST");
urlConnection.connect();
OutputStream outputStream = urlConnection.getOutputStream();
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
writer.write(data);
writer.close();
outputStream.close();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream(), "UTF-8"));
String line = null; StringBuilder sb = new StringBuilder();
while ((line = bufferedReader.readLine()) != null) {
sb.append(line);
}
bufferedReader.close();
result = sb.toString();
}
catch (UnsupportedEncodingException e) {
e.printStackTrace(); }
catch (IOException e) {
e.printStackTrace(); }
return result;
}
}
Antes de realizar la peticióm, compruebo la conexión a internet:
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo networkInfo = connectivityManager.getActiveNetworkInfo();
if (networkInfo != null && networkInfo.isConnected())
{
peticionAsync();
}
else
{
Toast toast2 = Toast.makeText(getApplicationContext(), "No hay conexión a INTERNET", Toast.LENGTH_SHORT);
toast2.show();
}
Esto aparece en el LOG:
W/System.err: javax.net.ssl.SSLHandshakeException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. W/System.err: at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:381) W/System.err: at libcore.net.http.HttpConnection.setupSecureSocket(HttpConnection.java:209) at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:478) W/System.err: at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433) W/System.err: at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:290) at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:240) W/System.err: at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:81) W/System.err: at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165) W/System.err: at midlab.Listado$PeticionAsync.POST(Listado.java:169) W/System.err: at midlab.Listado$PeticionAsync.doInBackground(Listado.java:94) W/System.err: at midlab.Listado$PeticionAsync.doInBackground(Listado.java:87) W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:287) W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:234) W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) at java.lang.Thread.run(Thread.java:856) W/System.err: Caused by: java.security.cert.CertificateException: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. W/System.err: at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkTrusted(TrustManagerImpl.java:276) at org.apache.harmony.xnet.provider.jsse.TrustManagerImpl.checkServerTrusted(TrustManagerImpl.java:197) W/System.err: at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.verifyCertificateChain(OpenSSLSocketImpl.java:597) W/System.err: at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_do_handshake(Native Method) at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl.startHandshake(OpenSSLSocketImpl.java:378) ... 16 more W/System.err: Caused by: java.security.cert.CertPathValidatorException: Trust anchor for certification path not found. W/System.err: ... 21 more
if (result.trim().equals("0"))