0

I included following permissions into Manifest:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

I am using this code:

try {
      java.net.URL url = new URL("http://www.temp372.000webhostapp.com/s.php?t=hello_there");
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
      e.printStackTrace();
      t1.setText("problem occured..");
}

Following is the PHP code:

<?php
$fn = "android.txt";
$data = "";
// get whatever they send and store it to the file.
if($_GET["t"])
{
    $data = $_GET["t"];
    if ($data !== '')
    {
        $myfile = fopen($fn, "a");
        fwrite($myfile, $data . "\n");
        fclose($myfile);
    }
}
?>

No errors are coming, I am trying running this app into bluestacks and my cellphone (ROG Phone). But results are same, no error or anything as textview is not setting and it just my PHP code is not receiving the information but when I try same URL into my web browser, PHP code runs cool.

jimmy
  • 117
  • 1
  • 7
  • 2
    Look at this : https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted The best solution is to use https instead http if the web site supports it – from56 Dec 01 '19 at 22:15
  • The request should use "background thread" you cant do this in a main thread. – Artem Dec 02 '19 at 02:35
  • Are you trying to just load the url in app? – Arnold Brown Dec 02 '19 at 06:24
  • Lluis I tried option #1 from the link you gave, but still not working, also I tried using POST methods but its not working too,Arnold I am not looking for response, I am just trying to give some data to the server. – jimmy Dec 02 '19 at 08:16
  • and now I am trying this code: WebView web1 = (WebView)findViewById(R.id.web); web1.loadUrl("temp372.000webhostapp.com/test.php"); but its not showing anything, I also have the permissions but I don't see the problem. Its the php page that just echo "Hello world", thats it, BUT Now when I tried webview to load "www.google.com", it worked fine. So, its blocking website? – jimmy Dec 02 '19 at 08:47
  • ok guys it worked, both with get and post method but only in background thread, thanks to Artem for this, and Lluis none of the options were required from the link you gave, It just I can't run that in main thread. – jimmy Dec 02 '19 at 09:01

2 Answers2

0

HTTP or "clear text" forbidden on default, you should allow it inside < application > in AndroidManifest.xml android:usesCleartextTraffic="true"

Artem
  • 303
  • 2
  • 9
  • the code worked well without adding this, But I ran the code in background thread, Also when I added this and trying to run into main thread, it wasn't working – jimmy Dec 02 '19 at 09:00
0

It worked well following are things I got: I don't have to add the following into manifest:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

I also didn't have to add any permission like android:usesCleartextTraffic="true" OR any network_security_config.xml in Manifest.

It worked when I ran the code into AsyncTask in Background thread like followings:

AsyncTask.execute(new Runnable() {
            @Override
            public void run() {
                HttpClient httpclient = new DefaultHttpClient();
                try {
                    httpclient.execute(new HttpGet("https://temp372.000webhostapp.com/s_get.php?t=MyWorld"));
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });

I thanks Artem for this and everyone else who were trying their best.

jimmy
  • 117
  • 1
  • 7