0

So, while looking up how to run a php script from an android app, I came across this code:

final String url = "http://example.com/creatd.php?nm=mytest";
    HttpClient client = new DefaultHttpClient();

    try {
        client.execute(new HttpGet(url));
        Intent intent = new Intent(this, Main.class);
        startActivity(intent);
    } catch(IOException e) {
        //do something here
    }

Originally, I assumed that this would work because the client.execute executes the script in the url. However, what I did not realize until now was the HttpGet gets information from running the script. My PHP script does not provide information. It merely executes a task on the server(correct me if I am wrong on the Httpclient).

How would I make it so the client.execute merely executes the script instead of also trying to get information from it? I am assuming the reason why my app crashes is since the script does not return any information the value is null.

PHP Code:

<?php
$str = $_GET['nm'];
mkdir($str,0700);
fopen($str."/".$str.".meb", "w");
file_put_contents($str."/".$str.".meb", "Hello, world!", FILE_APPEND | LOCK_EX);
?>

EDIT: Just a question, why do people keep downvoting my questions? I researched my question and tried to use solutions, but the solutions provided didn't work. What am I doing wrong?

user3864563
  • 365
  • 1
  • 7
  • 22

1 Answers1

1

Thanks to Ghost, I found a solution to the problem and added it in a thread.

final String url = "http://example.com/perform.php?nm="+string;
    new Thread() {
        @Override
        public void run() {
            try {
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(new HttpGet(url));
                StatusLine statusLine = response.getStatusLine();
                if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                    ByteArrayOutputStream out = new ByteArrayOutputStream();
                    try {
                        response.getEntity().writeTo(out);
                        out.close();
                    } catch (IOException e) {
                    }
                    String responseString = out.toString();
                    //..more logic
                } else {
                    //Closes the connection.
                    try {
                        response.getEntity().getContent().close();
                        throw new IOException(statusLine.getReasonPhrase());
                    } catch (IOException e) {
                    }
                }
            }
            catch (ClientProtocolException e)
            {

            }
            catch (IOException e)
            {

            }
        }
    }.start();

Again, thank you Ghost.

user3864563
  • 365
  • 1
  • 7
  • 22