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?

15
  • did u tried to execute ur php script in broswer? Commented Jul 25, 2014 at 6:40
  • Yes and it works fine. Commented Jul 25, 2014 at 6:40
  • ok what response u got in php script? Commented Jul 25, 2014 at 6:40
  • All it is supposed to do is create a folder and add a file to it. Commented Jul 25, 2014 at 6:42
  • It gets the name of the folder and file from using $_GET Commented Jul 25, 2014 at 6:43

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.