从Android向本地Web服务器发送GET http请求

时间:2012-04-04 20:00:07

标签: android http-get

我正在处理应用程序,我想将坐标存储到本地数据库(wampserver)所以我已经完成了PHP脚本并且它可以工作,但我的问题是从我执行该脚本.php应用HTTP GET方法。

我找到了这段代码,但它不起作用!!所以,我需要你的帮助,谢谢!

链接到HTTP获取代码 - > :http://www.softwarepassion.com/android-series-get-post-and-multipart-post-requests/

1 个答案:

答案 0 :(得分:6)

请更具体。什么不起作用?此代码可用于发出HTTP GET请求:

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
}