我做POST请求,服务器接收并记录GET请求

时间:2012-03-29 14:57:19

标签: php android post http-post

在Android应用程序中,Java执行POST请求,并使用php处理它。

在本地工作时工作正常,apache记录POST:

192.168.1.123 - - [29/Mar/2012:15:46:56 +0200] "POST /usuarioLogin.php HTTP/1.1" 200 292 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"

远程工作时,apache会记录GET请求,显然找不到发送的参数。 Apache记录以下内容:

78.XXX.256.XXX - - [29/Mar/2012:16:20:05 +0200] "GET /usuarioLogin.php HTTP/1.1" 200 267 "-" "Apache-HttpClient/UNAVAILABLE (java 1.4)"

执行POST的java代码:

private void HttpPost(String php, ArrayList<NameValuePair> nameValuePairs) {

    try {
        HttpClient httpclient = new DefaultHttpClient();
        String host = com.android.taggies.LoginUser.getContext().getResources().getString(R.string.host);
        HttpPost httppost = new HttpPost(host + php);
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();

        is = entity.getContent();

    } catch (Exception e) {
        Log.e("log_tag", "Error in http connection " + e.toString());
    }
}

PHP:

<?php
mysql_connect("localhost","root","");
mysql_select_db("dbBaggies");

$q=mysql_query("SELECT count(*) as 'return' FROM users
WHERE name='$_POST[user]' AND password ='$_POST[pass]'");

while($e=mysql_fetch_assoc($q))
{
    $output[]=$e;
} 

print(json_encode($output));

mysql_close();
?>

1 个答案:

答案 0 :(得分:0)

我也遇到POST请求问题,将图像上传到我的外部LAMP服务器。这真的对我有所帮助:

http://moazzam-khan.com/blog/?p=490

这是一个有几种GET和POST方法的类。试试这个:

public static HttpData post(String sUrl, Hashtable<String, String> ht) throws Exception {
            String key;
            StringBuffer data = new StringBuffer();
            Enumeration<String> keys = ht.keys();
            while (keys.hasMoreElements()) {
                    key = keys.nextElement();
                    data.append(URLEncoder.encode(key, "UTF-8"));
                    data.append("=");
                    data.append(URLEncoder.encode(ht.get(key), "UTF-8"));
                    data.append("&amp;");
            }
            return HttpRequest.post(sUrl, data.toString());
    }

我认为你需要一些外部课程。请检查链接。