0

I'm doing Android app. In that I'm passing values to PHP file. Before passing to PHP file I used log to see values at that time I got all values but while inserting data it is not submitted. I tried a lot but cant recognize where I went wrong.

Following is log which which is in log cat:

[{"MNO":"5656565664","Latitude":"17.3731701","Longitude":"78.504573"}]  

Following is doInBackground method where I used Log.e

@Override
protected Boolean doInBackground(String... arg0)
{
    Log.e("doInBackground", "doInBackground1");

    Log.e("arg0[0]", arg0[0]);

    ArrayList<NameValuePair> params = new ArrayList<NameValuePair>();    
    params.add(new BasicNameValuePair("Location", arg0[0]));

    Log.e("doInBackground", "doInBackground2");

    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.example.com/insertLocation.php");
    HttpParams httpParameters = new BasicHttpParams();
    httpclient = new DefaultHttpClient(httpParameters);

    Log.e("doInBackground", "doInBackground3");
    try {
        httppost.setEntity(new UrlEncodedFormEntity(params));
        HttpResponse response;
        response = httpclient.execute(httppost);        
        StatusLine statusLine = response.getStatusLine();        
        if (statusLine.getStatusCode() == HttpStatus.SC_OK) {

            Log.e("Google", "Server Responded OK");

        } else {

            response.getEntity().getContent().close();
            throw new IOException(statusLine.getReasonPhrase());
        }

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
}

Following is the insertLocation.php code where I'm trying to insert:

<?php 

$Location= $_POST['Location'];
$data = json_decode($Location, true);

$mno = $data ->MNO;
$latitude = $data ->Latitude;
$longitude =$data ->Longitude;


//Include db connect class
$hostname_localhost ="localhost";
$database_localhost ="geolocation";
$username_localhost ="abc";
$password_localhost ="abc123";
$localhost = mysql_connect($hostname_localhost,$username_localhost,$password_localhost) or trigger_error(mysql_error(),E_USER_ERROR);    
mysql_select_db($database_localhost, $localhost);

$flag['code']=0;
//Mysql inserting a new 

$result = mysql_query("INSERT INTO location(mno,latitude,longitude) VALUES('$mno','$latitude','$longitude')");  
if($result)
{
    $flag['code']=200;
} 
echo json_encode($flag);
mysql_close($con);
?>
1
  • while inserting data it is not submitted. What do you mean by that? The php script is not echoing the received data nor the lattitude and longitude. Start to do that. Forget about inserting in a database first. You should be sure first that you decode the json correct. So echo all. Retrieve the complete page in doInBackground to inspect if all is echoed correct. Now your code is blind. You do not see what is actually happening... Commented Nov 16, 2014 at 18:36

1 Answer 1

1

$data = json_decode($Location, true); this will return array

So

Use $data[0]['MNO']

Sign up to request clarification or add additional context in comments.

1 Comment

what error it throws ? What is output of var_dump($_POST) at start of php script ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.