Hi I'm having an issue POSTing a LineString from my Android Application to a geoPHP API. I'm pretty sure my code is working OK, I'm just not sure if it's possible to send a LineString as it is. Or if anyone knows any workarounds.
Thanks in Advance
Android Post Method
public String postData(LineString myLineString) throws IOException {
String response = "";
try {
URL url= new URL("myAPI");;
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(myLineString);//I know this is wrong as write() can only take type String as a parameter
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
String line;
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
while ((line=br.readLine()) != null) {
response+=line;
}
}
else {
response="";
}
} catch (Exception e) {
e.printStackTrace();
}
return response;
}
geoPHP method
$app->map ( "/linestring/(:id)", function ($elementID = null) use ($app)
{
$body = $app->request->getBody(); // get the body of the HTTP request (from client)
$geom = geoPHP::load('$body');
$insert_string = pg_escape_bytea($geom->out('ewkb'));
$sql = "INSERT INTO PATHS (geom) values (ST_GeomFromWKB('$insert_string'))";
try {
$db = getDB();
$stmt = pg_query($db, $sql);
$db = null;
}
catch(PDOException $e) {
//error_log($e->getMessage(), 3, '/var/tmp/phperror.log'); //Write error log
echo '{"error":{"text":'. $e->getMessage() .'}}';
}
} )->via( "POST");