Android url connection php get


Android url connection php get

嗨,我正在尝试发送一些参数到我的php服务器

我真的希望我的php服务器接收用户发送的参数并将它们存储在mysql数据库

如果我尝试连接http://somesite/store.php?Location=somewhere&temp=something使用chrome浏览器运行良好

但是如果我尝试在android上使用它。

我的数据库中没有更新记录

我有什么问题?

URL testUrl;
        try {
            testUrl = new URL("http://somesite.com/gcm_server/getdata.php?Location=bathroom&Humidity=25%&Temperature=27&Brightness=45");
            HttpURLConnection conn = (HttpURLConnection)testUrl.openConnection();
            InputStream in = conn.getInputStream();
            InputStreamReader isw = new InputStreamReader(in);
            int data = isw.read();
            while (data != -1) {
                char current = (char) data;
                data = isw.read();
                System.out.print(current);
            }

            Log.d(TAG, "testUrl= " + testUrl.toString());
            conn.disconnect();
        }catch(MalformedURLException ex){
            Log.e(TAG, ex.toString());
        }
        catch(IOException e){
            Log.e(TAG, "url connection Error");
        }

如果我尝试连接,然后我得到了日志url连接错误,但如果我试图连接一些网站(我覆盖了它与不同的网站)。我可以使用chrome浏览器连接

我不知道发生了什么事…

如果您使用的是本地web服务器,请确保使用10.0.2.2作为服务器名称而不是localhost ..

OK。首先。如果你要发送东西到服务器并存储它,你应该使用POST请求而不是GET。

我这里有一个小的例子来帮助你在Android中做到这一点:

    //These are your parameters
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    nameValuePairs.add(new BasicNameValuePair("param1","foo"));
    nameValuePairs.add(new BasicNameValuePair("param2","bar"));
    String result = "";
    InputStream is = null;
    HttpClient httpClient = new DefaultHttpClient();
    String urlPath = "http://somesite.com/somepage.php";
    try {
        HttpPost httpPost = new HttpPost(urlPath);
        httpPost.setEntity(new UrlEncodedFormEntity(params,     HTTP.UTF_8));
        BasicResponseHandler responseHandler = new BasicResponseHandler();
        result = httpClient.execute(httpPost, responseHandler);
    } catch (Exception e) {
        //Exception
    }
    //Your result is in the result variable now

这个例子是完全不完整的,你需要导入使用的东西,但希望你明白我在做什么。

您可以使用$_POST["theparam"];

在php服务器上获取参数