-1

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

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

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

4

1 回答 1

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
}
于 2012-04-04T20:08:02.063 回答