简体   繁体   中英

Error posting data with android

I am having an error that I can't seem to figure out why. I am trying to post data to a php file and am falling into a catch error. The toast I am getting in my catch is "Data was not sent", but I am getting the "Data was prepared" toast right before it. Could anybody take a look and help me identify what my problem is?

The android method:

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();
        }
    }

and my php file:

<?
$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();
?>

When you do not know where is problem try replace PHP script by

<?php
echo "OK";
?>

and try read response from PHP

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

When you get response OK problem is in PHP.

When i use httppost i use this code to get response. When i have response http request is OK.

        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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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