2012-04-04 126 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

相關問題