Hi, I'm trying to connect to my PHP file on the localhost server with my Android application using AsyncTask
. The problem is, when executing the application I got -1 as a response code and it (the debugger) doesn't state what the problem is.
I tried to apply the solution I found here, Android Https Status code -1, but it didn't work out for me.
PHPConnecteur.java
public class PHPConnecteur extends AsyncTask<String, Integer, String>{
private HashMap<String, String> parameters;
private String phpToCall;
@Override
protected String doInBackground(String... strings) {
//System.setProperty("http.keepAlive", "false");
try {
URL url = new URL("http://127.0.0.1:8888/api/test.php"); // here is your URL path
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000 );
conn.setConnectTimeout(15000 );
conn.setRequestMethod("GET");
//conn.setDoInput(true);
conn.setDoOutput(true);
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
InputStream input = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(input));
StringBuilder result = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
result.append(line);
}
// Pass data to onPostExecute method
return (result.toString());
}
else {
return conn.getResponseMessage();
}
}
catch(Exception e){
return "erreur de communication avec le serveur";
}
}
test.php
<?php
echo "Yeah, success!"
?>
I don't think the problem comes from PHP. So anyone know what the problem is? Any help is appreciated.