so, i'm working on project share location through android, i'm trying accomplish through posting latitude,longitude data through location api on android, mysql server using php script handle data sent android mysql. since i'm targeting sdk23, not able use standard apache library , have use httpurlconnection library provided android studio. when send data , receive on php , try store in mysql database, blank data gets stored. here's android code:
package com.example.sid.epics; import android.location.location; import android.os.asynctask; import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.menu; import android.view.menuitem; import android.util.log; import android.view.view; import android.widget.toast; import java.io.printstream; import java.net.url; import java.net.httpurlconnection; import java.net.urlencoder; import java.net.urlconnection; import com.google.android.gms.common.connectionresult; import com.google.android.gms.common.api.googleapiclient; import com.google.android.gms.common.api.googleapiclient.connectioncallbacks; import com.google.android.gms.common.api.googleapiclient.onconnectionfailedlistener; import com.google.android.gms.location.locationservices; import java.net.httpurlconnection; public class mainactivity extends appcompatactivity implements connectioncallbacks,onconnectionfailedlistener { protected static final string tag = "basic-location-sample"; protected googleapiclient mgoogleapiclient; protected location mlastlocation; protected string mlatitudetext; protected string mlongitudetext; protected java.net.url url; protected httpurlconnection conn; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); buildgoogleapiclient(); } protected synchronized void buildgoogleapiclient() { mgoogleapiclient = new googleapiclient.builder(this) .addconnectioncallbacks(this) .addonconnectionfailedlistener(this) .addapi(locationservices.api) .build(); } @override protected void onstart() { super.onstart(); mgoogleapiclient.connect(); } @override protected void onstop() { super.onstop(); if (mgoogleapiclient.isconnected()) { mgoogleapiclient.disconnect(); } } @override public void onconnected(bundle connectionhint) { // provides simple way of getting device's location , suited // applications not require fine-grained location , not need location // updates. gets best , recent location available, may null // in rare cases when location not available. mlastlocation = locationservices.fusedlocationapi.getlastlocation(mgoogleapiclient); if (mlastlocation != null) { mlatitudetext=(string.valueof(mlastlocation.getlatitude())); mlongitudetext=(string.valueof(mlastlocation.getlongitude())); } else { //toast.maketext(this, r.string.no_location_detected, toast.length_long).show(); toast.maketext(this,"no location detected",toast.length_long).show(); } } @override public void onconnectionfailed(connectionresult result) { // refer javadoc connectionresult see error codes might returned in // onconnectionfailed. log.i(tag, "connection failed: connectionresult.geterrorcode() = " + result.geterrorcode()); } @override public void onconnectionsuspended(int cause) { // connection google play services lost reason. call connect() // attempt re-establish connection. log.i(tag, "connection suspended"); mgoogleapiclient.connect(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.menu_main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. //int id = item.getitemid(); //noinspection simplifiableifstatement /* if (id == r.id.action_settings) { return true; }*/ return super.onoptionsitemselected(item); } public void notif(view view) { /* post online */ charsequence text = "notifcation toast"; int duration = toast.length_short; toast.maketext(getapplicationcontext(),mlatitudetext+" "+mlongitudetext,duration).show(); new myasynctask().execute(); } public void navb(view view) { /* launch map activity */ } private class myasynctask extends asynctask<string,void,double>{ @override protected double doinbackground(string... params){ makepost(); return null; } } public void makepost(){ int duration=toast.length_short; try { url = new url("http://collabu-sidshah.rhcloud.com/notif.php"); conn=(httpurlconnection)url.openconnection(); conn.setdooutput(true); conn.setrequestmethod("post"); string postdat="latitude"+ urlencoder.encode(mlatitudetext,"utf-8")+"longitude"+urlencoder.encode(mlongitudetext,"utf-8"); //string postdat=mlatitudetext; conn.setfixedlengthstreamingmode(postdat.getbytes().length); conn.setrequestproperty("content-type", "application/x-www-form-urlencoded"); java.io.bufferedoutputstream out = new java.io.bufferedoutputstream(conn.getoutputstream()); printstream pstream=new printstream(out); pstream.print(postdat); pstream.close(); } catch(java.net.malformedurlexception ex){ //toast.maketext(this, ex.tostring(), duration ).show(); } catch(java.io.ioexception ex){ //toast.maketext(this, ex.tostring(), duration).show(); } } }
and here's php script i'm running on openshift
<?php $location=array(); $parts = explode('&', $http_raw_post_data); foreach ( $parts $part ) { list($key, $value) = explode('=', $part, 2); for($x=0;$x<2;$x++){ $location[x]+=$_post[$key] = $value; } } $entitybody="\"".$location[0]." ".$$location[1]."\""; $conn = new mysqli('hostname', 'user', 'password', 'base'); if ($conn->connect_error) { die("connection failed: " . $conn->connect_error); } $sql="insert raw_data (raw) values ($entitybody)"; if ($conn->query($sql) === true) { echo "new record created successfully"; } else { echo "error: " . $sql . "<br>" . $conn->error; } $conn->close(); sleep(1);
the 1 thing stands out need format data properly, you're missing required &
, =
characters.
add them data string , should work, line breaks clarity:
string postdat = "latitude" + "=" + urlencoder.encode(mlatitudetext,"utf-8") + "&" + "longitude" + "=" + urlencoder.encode(mlongitudetext,"utf-8");
for more detailed info, take @ answer: how add parameters httpurlconnection using post
and blog post on subject: http://danielnugent.blogspot.com/2015/06/updated-jsonparser-with.html
Comments
Post a Comment