使用android发布数据时出错

时间:2014-10-08 04:22:12

标签: java php android sql post

我有一个错误,我似乎无法弄清楚原因。我试图将数据发布到php文件,并陷入catch错误。我收到的东西是"数据没有被发送"但是我得到了#34;数据准备好了#34;在它之前吐司。有人可以看看并帮我确定我的问题是什么吗?

android方法:

public void insertToDB(String data){
        try {
            HttpClient client=new DefaultHttpClient();
            HttpPost getMethod=new HttpPost("http://shawnc.webuda.com/upload.php");
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("RPM",data));
            getMethod.setEntity(new UrlEncodedFormEntity(nameValuePairs, HTTP.UTF_8));
            String toastText = "Data was prepared";
            Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT).show();
            try {
                client.execute(getMethod);
                toastText = "Data has been sent: " + data;
                Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT).show();
            }catch(Exception e){
                toastText = "Data was not sent";
                Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT).show();
            }
        }catch(Exception e){
            String toastText = "Data could not be prepared";
            Toast.makeText(getApplicationContext(), toastText, Toast.LENGTH_SHORT).show();
        }
    }

和我的php文件:

<?
$hostname_host ="MYSQLxxxxx.Smarterasp.net";
$database_host ="db_xxxxx_db";
$username_host ="9xxxxx_db";
$password_host ="password";

$server= mysql_connect($hostname_localhost,$username_localhost,$password_localhost)
or
trigger_error(mysql_error(),E_USER_ERROR);

mysql_select_db("db_xxxxx_db");
$sql=mysql_query("INSERT INTO driverdata (RPM)VALUES('".$_REQUEST['RPM']."')");

$r=mysql_query($sql);
if(!$r)
echo "Error in query: ".mysql_error();
mysql_close();
?>

1 个答案:

答案 0 :(得分:1)

如果您不知道问题出在哪里,请尝试按

替换PHP脚本
<?php
echo "OK";
?>

并尝试阅读PHP的响应

try {
     HttpResponse response = client.execute(getMethod);
     toastText = "Data has been sent: " + data;

当您收到回复时,问题出现在PHP中。

当我使用httppost时,我使用此代码来获得响应。当我有响应http请求没问题。

        HttpClient client = new DefaultHttpClient(httpParameters);
        client.getParams().setParameter("http.protocol.version", HttpVersion.HTTP_1_1);
        client.getParams().setParameter("http.socket.timeout", 2000);
        client.getParams().setParameter("http.protocol.content-charset", HTTP.UTF_8);
        httpParameters.setBooleanParameter("http.protocol.expect-continue", false);
        HttpPost request = new HttpPost("http://xxxxxx.com/upload.php");
        request.getParams().setParameter("http.socket.timeout", 5000);

        List<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("RPM",data));              

        try {
        UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters, HTTP.UTF_8);
        request.setEntity(formEntity);

        HttpResponse response = client.execute(request);
        //do with response
相关问题