Android Htppget URL Connection

Asked

Viewed 45 times

0

I’m trying to make an httpget connection from android to php, I made all php code connecting to mysql but starts giving error from after "urlConnection = ..."

protected String doInBackground(String... String username = (String) arg0[0];
String password = (String) arg0[1];
String urlString = "https://splitfz.000webhostapp.com/login.php?username=" + username + "&password=" + password;
StringBuffer chaine = new StringBuffer("");

URL url;
HttpURLConnection urlConnection = null;
try {
    url = new URL("https://splitfz.000webhostapp.com/login.php?username="+ username +"&password="+ password +"");

    urlConnection = (HttpURLConnection) url.openConnection();

    connection.setRequestProperty("User-Agent", "");
    connection.setRequestMethod("GET");
    connection.setDoInput(true);
    connection.connect();

    InputStream in = urlConnection.getInputStream();

  InputStreamReader isw = new InputStreamReader(in);

    int data = isw.read();
    while (data != -1) {
        char current = (char) data;
        data = isw.read();
        System.out.print(current);
    }
    this.statusField.setText("deu certo");
} catch (Exception e) {
    this.statusField.setText("deu erro");
    e.printStackTrace();
} finally {
    if (urlConnection != null) {
        urlConnection.disconnect();
    }
}

Return chaine; }

1 answer

2

I believe the problem lies in this excerpt of the code:

url = new URL("https://splitfz.000webhostapp.com/login.php?username="+ username +"&password="+ password +"");

Note that you are passing one https in URL, however you are using HttpURLConnection when it should be a HttpsURLConnection

For example:

url = new URL(https_url);
HttpsURLConnection con = (HttpsURLConnection)url.openConnection();

You can read about the related methods of each of them in:

Httpurlconnection

Httpsurlconnection

If you need an example, this can help you:

Java Httpsurlconnection example

Browser other questions tagged

You are not signed in. Login or sign up in order to post.