doumu1873 2016-09-28 06:39
浏览 41

我无法在Android应用中更新经度和纬度的数据

Here is my code

1.Java Code

class makePOST extends AsyncTask<String, Void, Double> {
    int duration = Toast.LENGTH_SHORT;
    @Override
    protected void onPreExecute() {
        Toast.makeText(update.this, "onPreExecute", Toast.LENGTH_LONG).show();
    }
    protected Double doInBackground(String... params) {
            try {
                url = new URL("http://student.96.lt/Android/update.php");
                conn = (HttpURLConnection) url.openConnection();
                conn.setDoOutput(true);
                conn.setDoInput(true);
                conn.setRequestMethod("POST");
                String postdat = "code" + "=" + URLEncoder.encode(mCode, "UTF-8") + "&" + "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");
                OutputStream OS = conn.getOutputStream();
                InputStream in = conn.getInputStream();

                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
                bufferedWriter.write(postdat);
                bufferedWriter.flush();
                // Get output
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                StringBuilder sb = new StringBuilder();
                String line;
                while ((line = br.readLine()) != null) {
                    sb.append(line);
                    sb.append("
");
                }

                in.close();
                bufferedWriter.close();
//            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();
            }
            return null;
        }

    };  

2. PHP Code(update.php)

<?php
include 'connect.php';
$m_lat=$_POST['latitude'];
$m_lng=$_POST['longitude'];
$m_code=$_POST['code'];
$query="update `location` set `longitude`='$m_lng',`latitude`='$m_lat' where `code`='$m_code'";
mysqli_query($connect,$query);
mysqli_close($connect);
?>

3. AndroidManifest.xml File

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.project.nikhil.gpslocation">
<permission
    android:name="com.javapapers.currentlocationinmap.permission.MAPS_RECEIVE"
    android:protectionLevel="signature" />

<uses-permission android:name="com.javapapers.currentlocationinmap.permission.MAPS_RECEIVE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

<uses-feature
    android:glEsVersion="0x00020000"
    android:required="true" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <uses-feature
        android:name="android.hardware.location.gps"
        android:glEsVersion="0x00020000"
        android:required="true" />

    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

    <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />
    <meta-data
        android:name="com.google.android.maps.v2.API_KEY"
        android:value="MyKey" />
    <activity
        android:name=".update"
        android:label="@string/title_activity_update"
        android:exported="true"
        android:theme="@style/AppTheme.NoActionBar">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

</manifest>

And I also want to update longitude and latitude every 10 seconds. This is my first app. I'm at beginning level of Android development.

Now my PHP code can be seen at: student.96.lt/Android/test.php

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100