send GET http request from Android to local web server

Go To StackoverFlow.com

-1

I'm working on application that i want to store the coordinates to a local Database (wampserver) so I've done the php script and it works , but my problem is to execute that script.php from my app with HTTP GET method.

I've found this code but it doesn't work !! so please i need your help and thank you!

Link to the HTTP Get code --> : http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/

2012-04-04 20:00
by Jamel Naghmouchi


6

Please be more specific. What doesn't work? This code is what you can use to make an HTTP GET request:

HttpClient httpClient = new DefaultHttpClient();  
String url = "http://www.mywebsite.com/script.php";
HttpGet httpGet = new HttpGet(url);
try {
    HttpResponse response = httpClient.execute(httpGet);
    StatusLine statusLine = response.getStatusLine();
    if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
        HttpEntity entity = response.getEntity();
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        entity.writeTo(out);
        out.close();
        String responseStr = out.toString();
        // do something with response 
    } else {
        // handle bad response
    }
} catch (ClientProtocolException e) {
    // handle exception
} catch (IOException e) {
    // handle exception
}
2012-04-04 20:08
by Tyler Treat
i use wampserver so do i must replace the url with "http://10.0.2.2/folder/script.php?value1=something&value2=something" does this url correct ? - Jamel Naghmouchi 2012-04-04 20:59
@Jamel: yes, if you're using the emulator localhost is 10.0.2.2. Don't forget to add the INTERNET permission to AndroidManifest.xml - Tyler Treat 2012-04-04 21:07
ok i will try this code and hope that will work : - Jamel Naghmouchi 2012-04-04 21:16
Good luck! Let me know how it goes - Tyler Treat 2012-04-04 21:16
i'm stuck in another problem after i pasted your code, it complains about some error in line #30 and #35 and # 36 and it says "Surround with Try/Catch" ?? im newer to Android developmenti've done this on iphone very simply!! so please help me !

this is my code --> : http://pastebin.com/L9SndTH - Jamel Naghmouchi 2012-04-04 22:06

I excluded the try-catch block for brevity. Java has a notion of checked exceptions, which are exceptions that MUST be handled, either by re-throwing or catching with a try-catch. I have updated my code to reflect this - Tyler Treat 2012-04-04 22:11
now it says unfortunately, app has stopped !!? : - Jamel Naghmouchi 2012-04-04 22:44
Will need more information, like a stack trace - Tyler Treat 2012-04-04 23:04
i've tried another code and seems to be same problem this the link for the code --> : http://pastebin.com/a9kKyaQ - Jamel Naghmouchi 2012-04-05 09:21
and this is the log messages --> : http://www.mediafire.com/?eqvda02al3nrc5 - Jamel Naghmouchi 2012-04-05 09:23
Ads