Run a PHP script through an Android application

I have a PHP script stored on a Xampp server and want my application to execute it to complete the task. In eclipse, nothing happened with android.txt on the server side. Below is the code for my Android app.

String url = "http://my.site.php?data=hello";
    HttpClient client = new DefaultHttpClient();
    try {
      client.execute(new HttpGet(url));
    } catch(IOException e) {
      //do something here
    }

code>

the next one is my server side php code.

 $name=$_GET['data'];
    $file=fopen("./android.txt","w");
    fwrite($file, $name);
    fclose($file);

code> Although I run php from mozila browser, it works fine. but this code does not work for android.

+4
source share
1 answer

- , , , . AsyncTask, . , EditText , , .

 URL url = "http://my.site.php?data=hello"
 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("GET");

        String line;
        StringBuilder sb= new StringBuilder();
        BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        while ((line=br.readLine()) != null) {
            sb.append(line);
            response =sb.toString();
        }
        br.close();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return response;
+2

Source: https://habr.com/ru/post/1623517/


All Articles