简体   繁体   English

将数据从android发布到php

[英]Post data from android to php

I have an android application that i using to post data to a website using http post but it is not posting any data to the website. 我有一个Android应用程序,我使用它使用http post将数据发布到网站,但它没有将任何数据发布到网站。

In my android code: 在我的android代码中:

                   postParameters.add(new BasicNameValuePair("latitude", Double.toString(latitude)));  
                   postParameters.add(new BasicNameValuePair("longitude", Double.toString(longitude))); 
                   response = CustomHttpClient.executeHttpPost("url", postParameters);  

In executeHttpPost method 在executeHttpPost方法中

public static String executeHttpPost(String url, ArrayList postParameters)
        throws Exception {
    BufferedReader in = null;
    try {
        HttpClient client = getHttpClient();
        HttpPost request = new HttpPost(url);
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(
                postParameters);
        request.setEntity(formEntity);
        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 result = sb.toString();
        return result;
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

Here is the php source 这是PHP源

$lat=$_POST['latitude'];  
$long=$_POST['longitude'];  


mysql_connect('localhost','welcome','Welcome123') or die('conn error');
mysql_select_db('es')  or die('select error');

$query = "insert into GEO_LOC (GEO_LOC_LAT,GEO_LOC_LONG) values ($lat,$long)";
$result = mysql_query($query);

if($result)
{
echo 1;
}
else echo "Database Error";
?>

But this post method does not do anything . 但是此post方法没有任何作用。 Am i missing something? 我想念什么吗? Do i have to add anything additional? 我是否需要添加任何其他内容?

try this, 尝试这个,

HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(ADD URL HERE);
    try {
        List<NameValuePair> name = new ArrayList<NameValuePair>();

        name.add(new BasicNameValuePair("latitude", Double.toString(latitude))); 
        name.add(new BasicNameValuePair("longitude", Double.toString(longitude)));

        post.setEntity(new UrlEncodedFormEntity(name));
        HttpResponse response = client.execute(post);
        BufferedReader rd = new BufferedReader(new InputStreamReader(
                response.getEntity().getContent()));
        String line = "";
        StringBuilder sb = new StringBuilder();
        while ((line = rd.readLine()) != null) {
            sb.append(line + "\n");
        }
} catch (Exception e) {

    }

check below code: 检查以下代码:

public static ArrayList<NameValuePair> arrayPost = new ArrayList<NameValuePair>();

add parameter in above array if array size is 0 then, 如果数组大小为0,则在上面的数组中添加参数,

if (arrayPost.size() <= 0) {
                arrayPost.add(new BasicNameValuePair("test", "test"));
            }

public static String Call_Http_URL_PostMethod(String url)
            throws ClientProtocolException, IOException {

        String is = null;
        try {

            if (arrayPost.size() <= 0) {
                arrayPost.add(new BasicNameValuePair("test", "test"));
            }

            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            httppost.setEntity(new UrlEncodedFormEntity(arrayPost, HTTP.UTF_8));
            HttpResponse responce = httpclient.execute(httppost);
            HttpEntity entity = responce.getEntity();
            is = EntityUtils.toString(entity);
            Log.e("post responce----->", "" + is);
        } catch (Exception e) {
            Log.d("post responce error ----->", "" + e.getMessage().toString());
            is = null;
        }
        return is;
    }

change your php code like below(this is just for testing) 更改您的php代码,如下所示(这仅用于测试)

if($result)
{
echo json_encode("result"=array("sucsess"););
}

echo json_encode("result"=array("Database Error"));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM