Android HttpGet请求插入Web数据库

Android HttpGet请求插入Web数据库,android,http-get,Android,Http Get,我正在编写一个应用程序,用户输入歌曲信息,然后在网站上查看。我正在尝试使此HttpGet请求生效。我真的不需要服务器返回任何信息。我只需要将信息存储在MySQL数据库中。在php方面,我使用$\u GET获取信息。我是不是走错了路?以下是我的android代码: public void executeHttpGet() throws Exception{ BufferedReader in = null; try { HttpClient client = new

我正在编写一个应用程序,用户输入歌曲信息,然后在网站上查看。我正在尝试使此HttpGet请求生效。我真的不需要服务器返回任何信息。我只需要将信息存储在MySQL数据库中。在php方面,我使用$\u GET获取信息。我是不是走错了路?以下是我的android代码:

public void executeHttpGet() throws Exception{
    BufferedReader in = null;
    try {
        HttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(
              "http://localhost:8888/?title=hello&artist=horray");
        HttpResponse response = client.execute(request);
        in = new BufferedReader
        (new InputStreamReader(response.getEntity().getContent()));
        StringBuffer sb = new StringBuffer("");
        String line = "";
        String NL = System.getProperty("line.separator");
        while ((line = in.readLine()) != null) {
            sb.append(line + NL);
        }
        in.close();
        String page = sb.toString();
        System.out.println(page);
        } finally {
        if (in != null) {
            try {
                in.close();
                } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

为什么不试着用帖子来代替呢?看看这篇文章:


你真的应该使用POST而不是GET。您必须更改您的PHP,但由于您要将数据插入数据库,因此不应使用GET。这正是这篇文章的目的

另外,“localhost”也不起作用,因为在模拟器上运行时,“localhost”指的是手机。在模拟器上,“10.0.2.2”表示您的计算机。所以我想这就是你想用的

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("10.0.2.2:8888");

try {
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("title", "hello"));
    params.add(new BasicNameValuePair("artist", "horray"));
    httpPost.setEntity(new UrlEncodedFormEntity(params));

    httpClient.execute(httpPost);
    Log.i("posting", "Saved to server");
} catch (Exception e) {
    Log.e("posting", e.getMessage());
}
HttpClient-HttpClient=newdefaulthttpclient();
HttpPost HttpPost=新的HttpPost(“10.0.2.2:8888”);
试一试{
List params=new ArrayList();
添加(新的BasicNameValuePair(“标题”、“您好”);
参数添加(新的BasicNameValuePair(“艺术家”、“horray”);
setEntity(新的UrlEncodedFormEntity(参数));
httpClient.execute(httpPost);
Log.i(“发布”、“保存到服务器”);
}捕获(例外e){
Log.e(“posting”,e.getMessage());
}
希望有帮助