dth2331 2016-03-03 00:11
浏览 94

从Android到PHP的POST LineString

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");
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 高德地图点聚合中Marker的位置无法实时更新
    • ¥15 DIFY API Endpoint 问题。
    • ¥20 sub地址DHCP问题
    • ¥15 delta降尺度计算的一些细节,有偿
    • ¥15 Arduino红外遥控代码有问题
    • ¥15 数值计算离散正交多项式
    • ¥30 数值计算均差系数编程
    • ¥15 redis-full-check比较 两个集群的数据出错
    • ¥15 Matlab编程问题
    • ¥15 训练的多模态特征融合模型准确度很低怎么办