2012-04-04 128 views
-1

我正在处理应用程序,我想将坐标存储到本地数据库(wampserver),所以我已经完成了php脚本,它的工作原理,但我的问题是使用HTTP GET方法从我的应用程序执行script.php。发送来自Android的GET请求到本地Web服务器

我发现了这段代码,但它不工作!所以请我需要你的帮助,谢谢你!

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

回答

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 
} 
+0

我wampserver使用所以我必须以“http://10.0.2.2/folder/script.php?value1=something&value2=更换网址东西“这个网址是否正确? – 2012-04-04 20:59:02

+0

@Jamel:是的,如果你使用的是模拟器localhost是10.0.2.2。不要忘记将INTERNET权限添加到“AndroidManifest.xml”中。 – 2012-04-04 21:07:22

+0

好吧,我会尝试这段代码,并希望这将工作:) – 2012-04-04 21:16:13

相关问题