创建一个空Android项目
如果想要在局域网外也可以访问提交数据可以用内网穿透。
导入百度地图SDK
百度地图开放平台 http://lbsyun.baidu.com/
百度地图Android定位SDK文档链接 http://lbsyun.baidu.com/index.php?title=android-locsdk
注册和获取密匙(AK)
登录页面,没账号就注册一个 https://passport.baidu.com/v2/?login&fr=old&login&u=http://lbsyun.baidu.com/index.php?title=%E9%A6%96%E9%A1%B5
然后
SHA1
那个如何获取可以看官方文档
先确定.android文件夹位置,一般都在这
然后
如果下面没有就从
这打开
然后cd C:UsersAdministrator.android
对了,得先跳到C盘
然后再输入 keytool -list -v -keystore debug.keystore
默认的是android
就可以了
提交获得AK
配置环境
这个就看那个开发文档就好
要注意
这里的这个要放到android{ }里面
sourceSets{
main{
jniLibs.srcDir 'libs'
jni.srcDirs = [] //disable automatic ndk-build
}
}
最后我的
build.gradle文件
plugins {
id 'com.android.application'
}
android {
compileSdkVersion 30
buildToolsVersion "30.0.3"
defaultConfig {
applicationId "com.example.myapplication"
minSdkVersion 23
targetSdkVersion 30
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
sourceSets {
main {
jniLibs.srcDir 'libs'
jni.srcDirs = [] //disable automatic ndk-build
}
}
}
dependencies {
implementation fileTree(dir: 'libs' , include: ['*.jar'])
androidTestCompile ('com.android.support.test.espresso: espresso-core: 2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
testImplementation 'junit: junit: 4.12'
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'com.google.android.material:material:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation files('libs\BaiduLBS_Android.jar')
testImplementation 'junit:junit:4.+'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
}
AndroidManifest.xml
到这环境就配置完了
在手机上跑的时候记得给权限,不然定位返回的结果是null
代码
Android
package com.alice.web; import android.util.Log; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; /** * 使用get方法获取Http服务器数据 */ public class WebServiceGet { public static String executeHttpGet(String name,String temper,String time,String location){ HttpURLConnection connection = null; InputStream in = null; try{ String Url = "http://192.168.10.11:8888/AndroidSerivce_war_exploded/LogLet/get"; String path = Url + "?name=" + name + "&temper=" + temper+"&time="+time+"&location="+location; System.out.println(path); try { URL url = new URL(path); connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(10000);//建立连接超时 connection.setReadTimeout(8000);//传递数据超时 in = connection.getInputStream(); return parseInfo(in); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }catch (Exception e){ e.printStackTrace(); }finally { //意外退出时,连接关闭保护 if(connection != null){ connection.disconnect(); } if(in != null){ try{ in.close(); }catch (Exception e){ e.printStackTrace(); } } } return null; } //得到字节输入流,将字节输入流转化为String类型 public static String parseInfo(InputStream inputStream){ BufferedReader reader = null; String line = ""; StringBuilder response = new StringBuilder(); try { reader = new BufferedReader(new InputStreamReader(inputStream)); while((line = reader.readLine()) != null){ response.append(line); } return response.toString(); }catch (Exception e){ e.printStackTrace(); }finally { if(reader != null){ try{ reader.close(); }catch (Exception e){ e.printStackTrace(); } } } return null; } }
package com.example.myapplication; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.provider.ContactsContract; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; import com.baidu.location.BDAbstractLocationListener; import com.baidu.location.BDLocation; import com.baidu.location.LocationClient; import com.baidu.location.LocationClientOption; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; import static com.example.myapplication.MyLocationListener.locat; public class MainActivity extends AppCompatActivity { private EditText name,number; private Button button; //*******************// public LocationClient mLocationClient = null; private MyLocationListener myListener = new MyLocationListener(); //BDAbstractLocationListener为7.2版本新增的Abstract类型的监听接口 //原有BDLocationListener接口暂时同步保留。具体介绍请参考后文中的说明 //**************************// public boolean verifyString(String nam,String tem){ if(nam==null||nam.isEmpty()||tem==null||tem.isEmpty()){ Toast.makeText(getApplicationContext(),"姓名或体温为空,请重新填写",Toast.LENGTH_SHORT).show(); return false; }else { return true; } } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); name=findViewById(R.id.name); number=findViewById(R.id.temper); button=findViewById(R.id.button); //************************// mLocationClient = new LocationClient(getApplicationContext()); //声明LocationClient类 mLocationClient.registerLocationListener(myListener); //注册监听函数 LocationClientOption option = new LocationClientOption(); option.setIsNeedAddress(true); //可选,是否需要地址信息,默认为不需要,即参数为false //如果开发者需要获得当前点的地址信息,此处必须为true option.setNeedNewVersionRgc(true); //可选,设置是否需要最新版本的地址信息。默认需要,即参数为true mLocationClient.setLocOption(option); //mLocationClient为第二步初始化过的LocationClient对象 //需将配置好的LocationClientOption对象,通过setLocOption方法传递给LocationClient对象使用 //更多LocationClientOption的配置,请参照类参考中LocationClientOption类的详细说明 //*******************************// mLocationClient.start(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String nam=name.getText().toString(); String tem=number.getText().toString(); System.out.println(nam+"@@@"+tem); if(verifyString(nam,tem)) { MyThread myThread=new MyThread(); myThread.setData_Name_Temper_Location(nam,tem,locat); new Thread(myThread).start(); System.out.println(locat); } } }); } }
package com.example.myapplication; import com.baidu.location.BDAbstractLocationListener; import com.baidu.location.BDLocation; public class MyLocationListener extends BDAbstractLocationListener { public static String locat; @Override public void onReceiveLocation(BDLocation location) { //此处的BDLocation为定位结果信息类,通过它的各种get方法可获取定位相关的全部结果 //以下只列举部分获取地址相关的结果信息 //更多结果信息获取说明,请参照类参考中BDLocation类中的说明 String addr = location.getAddrStr(); //获取详细地址信息 String country = location.getCountry(); //获取国家 String province = location.getProvince(); //获取省份 String city = location.getCity(); //获取城市 String district = location.getDistrict(); //获取区县 String street = location.getStreet(); //获取街道信息 String adcode = location.getAdCode(); //获取adcode String town = location.getTown(); //获取乡镇信息 locat=addr; } }
package com.example.myapplication; import com.alice.web.WebServiceGet; import java.text.SimpleDateFormat; import java.util.Date; import java.util.TimeZone; public class MyThread implements Runnable{ private String name; private String temper; private String time; private String location; public String getTime(){ SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd HH:mm:ss"); //设置时间格式 formatter.setTimeZone(TimeZone.getTimeZone("GMT+08")); //设置时区 Date curDate = new Date(System.currentTimeMillis()); //获取当前时间 String createDate = formatter.format(curDate); //格式转换 return createDate; } public void setData_Name_Temper_Location(String name,String temper,String location){ this.location=location; this.name=name; this.temper=temper; this.time=getTime(); } @Override public void run() { WebServiceGet.executeHttpGet(name,temper,time,location); } }
布局异常简陋
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:ems="10" android:inputType="textPersonName" android:text="@string/name" app:layout_constraintBottom_toTopOf="@+id/guideline" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/guideline" /> <androidx.constraintlayout.widget.Guideline android:id="@+id/guideline" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" app:layout_constraintGuide_percent="0.2" /> <EditText android:id="@+id/temper" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="116dp" android:ems="10" android:inputType="number" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="@+id/name" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="36dp" android:text="@string/确定" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/temper" /> </androidx.constraintlayout.widget.ConstraintLayout>
web
我在学ssm框架所以用的ssm框架,不用感觉更简单
核心就是
从这的url里取得参数在写入数据库
已经今天两点了,明天在继续
晚上神志不清命名异常离谱
先是祖传配置文件
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <import resource="springMapper.xml"/> <import resource="springService.xml"/> <import resource="springMVC.xml"/> </beans>
jdbc.user=root jdbc.password=123456 #jdbc.driverClass=com.mysql.cj.jdbc.Driver jdbc.driverClass=com.mysql.jdbc.Driver jdbc.jdbcUrl=jdbc:mysql://localhost:3307/ssmbuild?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=false&serverTimezone=UTC
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE configuration PUBLIC "-//mybatis.org//DTD Config 3.0//EN" "http://mybatis.org/dtd/mybatis-3-config.dtd"> <configuration> <typeAliases> <package name="com.alice.pojo"/> </typeAliases> </configuration>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd"> <!-- 配置整合mybatis --> <!-- 1.关联数据库文件 --> <context:property-placeholder location="classpath:database.properties"/> <!-- 2.数据库连接池 --> <!--数据库连接池 dbcp 半自动化操作 不能自动连接 c3p0 自动化操作(自动的加载配置文件 并且设置到对象里面) --> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"> <!-- 配置连接池属性 --> <property name="driverClass" value="${jdbc.driverClass}"/> <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/> <property name="user" value="${jdbc.user}"/> <property name="password" value="${jdbc.password}"/> <!-- c3p0连接池的私有属性 --> <property name="maxPoolSize" value="30"/> <property name="minPoolSize" value="10"/> <!-- 关闭连接后不自动commit --> <property name="autoCommitOnClose" value="false"/> <!-- 获取连接超时时间 --> <property name="checkoutTimeout" value="10000"/> <!-- 当获取连接失败重试次数 --> <property name="acquireRetryAttempts" value="2"/> </bean> <!-- 3.配置SqlSessionFactory对象 --> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource"/> <!-- 配置MyBaties全局配置文件:mybatis-config.xml --> <property name="configLocation" value="classpath:mybatis-config.xml"/> </bean> <!-- 4.配置扫描Dao接口包,动态实现Dao接口注入到spring容器中 --> <!--解释 :https://www.cnblogs.com/jpfss/p/7799806.html--> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <!-- 注入sqlSessionFactory --> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> <!-- 给出需要扫描Dao接口包 --> <property name="basePackage" value="com.alice.mapper"/> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd"> <!-- 配置SpringMVC --> <!-- 1.开启SpringMVC注解驱动 --> <mvc:annotation-driven /> <!-- 2.静态资源默认servlet配置--> <mvc:default-servlet-handler/> <!-- 3.配置jsp 显示ViewResolver视图解析器 --> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> <property name="prefix" value="/WEB-INF/jsp/" /> <property name="suffix" value=".jsp" /> </bean> <!-- 4.扫描web相关的bean --> <context:component-scan base-package="com.alice.controller" /> </beans>
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 扫描service相关的bean --> <context:component-scan base-package="com.alice.service" /> <!--注入到IOC容器中--> <bean id="androidServiceImpl" class="com.alice.service.androidserviceServiceImpl"> <property name="androidserviceMapper" ref="androidserviceMapper"/> </bean> <!-- 配置事务管理器 --> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <!-- 注入数据库连接池 --> <property name="dataSource" ref="dataSource" /> </bean> </beans>
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="https://jakarta.ee/xml/ns/jakartaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_5_0.xsd" version="5.0"> <!--DispatcherServlet--> <servlet> <servlet-name>DispatcherServlet</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <!--一定要注意:我们这里加载的是总的配置文件,之前被这里坑了!--> <param-value>classpath:applicationContext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>DispatcherServlet</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> <!--encodingFilter--> <filter> <filter-name>encoding</filter-name> <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encoding</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <filter> <filter-name>encodingFilter</filter-name> <filter-class> org.springframework.web.filter.CharacterEncodingFilter </filter-class> <init-param> <param-name>encoding</param-name> <param-value>utf-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Session过期时间--> <session-config> <session-timeout>15</session-timeout> </session-config> </web-app>
然后是代码
package com.alice.service; import com.alice.pojo.AndroidSerivceData; import java.util.List; public interface androidserviceService { List<AndroidSerivceData> getAll(); List<AndroidSerivceData> selectByTime(String time); List<AndroidSerivceData> selectByName(String name); List<AndroidSerivceData> selectBylocation(String location); int addData(AndroidSerivceData androidSerivceData); }
package com.alice.service; import com.alice.mapper.androidserviceMapper; import com.alice.pojo.AndroidSerivceData; import java.util.List; public class androidserviceServiceImpl implements androidserviceService{ androidserviceMapper androidserviceMapper; public void setAndroidserviceMapper(androidserviceMapper androidserviceMapper){ this.androidserviceMapper=androidserviceMapper; } @Override public List<AndroidSerivceData> getAll() { return androidserviceMapper.getAll(); } @Override public List<AndroidSerivceData> selectByTime(String time) { return androidserviceMapper.selectByTime(time); } @Override public List<AndroidSerivceData> selectByName(String name) { return androidserviceMapper.selectByName(name); } @Override public List<AndroidSerivceData> selectBylocation(String location) { return androidserviceMapper.selectBylocation(location); } @Override public int addData(AndroidSerivceData androidSerivceData) { return androidserviceMapper.addData(androidSerivceData); } }
package com.alice.pojo; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor public class AndroidSerivceData { private String name; private String temper; private String time; private String location; }
package com.alice.mapper; import com.alice.pojo.AndroidSerivceData; import org.apache.ibatis.annotations.Insert; import org.apache.ibatis.annotations.Select; import java.util.List; public interface androidserviceMapper { @Select("select * from androidservice") List<AndroidSerivceData> getAll(); @Select("select * from androidservice where time like concat('%',#{time},'%')") List<AndroidSerivceData> selectByTime(String time); @Select("select * from androidservice where name like concat('%',#{name},'%')") List<AndroidSerivceData> selectByName(String name); @Select("select * from androidservice where location like concat('%',#{location},'%')") List<AndroidSerivceData> selectBylocation(String location); @Insert("insert into androidservice(name,temper,time,location) values(#{name},#{temper},#{time},#{location})") int addData(AndroidSerivceData androidSerivceData); }
package com.alice.controller; import com.alice.pojo.AndroidSerivceData; import com.alice.service.androidserviceService; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping("/LogLet") public class getData { @Autowired @Qualifier("androidServiceImpl") private androidserviceService service; @RequestMapping("/get") @ResponseBody public String get(String name,String temper,String time,String location){ System.out.println(name+":"+temper+":"+time+":"+location); int i=service.addData(new AndroidSerivceData(name,temper,time,location)); System.out.println(i); return "msg"; } }