0

I am new to Android development. I want to test connect my app with PHP. I googled a lot, but they were all complex.

First, I want to learn the connection and retrieving only one simple text like 'Hello world' and print it out in my Android app.

What would be an example or a link of simple example?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
rabar kareem
  • 630
  • 9
  • 28
  • 1
    see http://stackoverflow.com/questions/3505930/make-an-http-request-with-android . You only need to make an HTTP request to the `php` file, and use the output. – Nick Andriopoulos Mar 26 '13 at 17:20

2 Answers2

2

Here is an example to start working with. It's just a copy-paste, but it should work.

The output of your request is stored in myString.

Oh, never perform such operations on the GUI thread; run them in a separate thread.

// An the Android manifest, add:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>


String myString;
String url = "http://www.yoursite.com/yourphpscript.php";
HttpClient httpclient = new DefaultHttpClient();

HttpGet request;
try {
    request = new HttpGet(new URI(url));

    request.addHeader("User-Agent", "Android");


    HttpResponse response = httpclient.execute(request);

    StatusLine statusLine = response.getStatusLine();
    if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        response.getEntity().writeTo(out);
        out.close();
        myString = out.toString();
    }
}
catch (URISyntaxException e1) {

    e1.printStackTrace();
}
catch (ClientProtocolException e) {

    e.printStackTrace();
}
catch (IOException e) {

    e.printStackTrace();
}
catch (Exception e) {
    e.printStackTrace();
}
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
lelloman
  • 13,883
  • 5
  • 63
  • 85
  • thanks for your reply. but every time when i run this code I get a NullExceptionPointer exception , how to solve this, even when I run it on the new Thread – rabar kareem Mar 27 '13 at 14:46
  • wich line raises the Null(Exception)-><-(Pointer)? btw did you add the permission in android manifest? – lelloman Mar 27 '13 at 15:36
  • I forgot to mention that I'm making a connection with localhost on my computer , I use this url http://10.0.2.2/foler/login.php, I think here is the exception – rabar kareem Mar 27 '13 at 15:47
  • And I have added the manifest permission in the manifest file – rabar kareem Mar 27 '13 at 15:54
  • mmm localhost should not be a problem, what I'd need to know is, what raises the nullpointerexception? you should read in the logacat output the line of code where the bomb exploded and then go to look in your code for that line and tell me what's written there – lelloman Mar 27 '13 at 16:00
  • I did some changing now I got this exception 03-27 16:13:43.898: W/System.err(1925): java.net.URISyntaxException: Illegal character in authority at index 15: http://10.0.2.2 /folder/login.php – rabar kareem Mar 27 '13 at 16:15
  • yes its like this "http : //10.0.2.2 /folder/login.php" but I also could not access this url with emulators browser – rabar kareem Mar 27 '13 at 16:21
  • ok...so the problem is not your code, it's the emulator that has no access to the internet. are you connect behind a proxy? – lelloman Mar 27 '13 at 16:35
  • The emulator has access to the internet, I can browse other sites with , but with my local server doesn't work, I tried the computer Ip address as well ,it says 'Forbiddin You dont have permission to access on this server' – rabar kareem Mar 27 '13 at 16:49
  • I see...so this is another issue, isn't it? I mean, we're moving quite far away from this question's topic... – lelloman Mar 27 '13 at 17:00
  • sorry but they all about the problem in this question – rabar kareem Mar 27 '13 at 17:14
  • Mate, I'm currently developing an android REST application, while developing I'm using a local web server instead of the remote actual one because of response latency, but the code is 100% the same, except for urls...so, your problem now is not "retrieve a simple text fomr php to android" anymore but rather "can't access locale web server from android", don't you agree? – lelloman Mar 27 '13 at 17:23
  • I think you're right ,you mean I should post this question this time. at all thank you for your time and help. – rabar kareem Mar 27 '13 at 17:33
0

Java Code Geeks has a great article on parsing JSON with GSON (library), Android JSON Parsing with Gson Tutorial. It will get you all the connectivity bits you need as well as how to expand your solution should you start offering "hello world" up as a web service.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bill Mote
  • 12,644
  • 7
  • 58
  • 82