[英]android studio app keeps stopping when posting json
我想知道两个坐标之间的距离。
当坐标发生变化时,我想知道它们之间的距离(以米为单位)。
代码主活动:
private TextView textView;
private TextView label3,label2;
private EditText editText;
protected LocationManager locationManager;
private String name;
double latitude;
double longitude;
RequestQueue queue;
double Lan;
double Lon;
@SuppressLint("MissingPermission")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
label3 = (TextView)findViewById(R.id.label3);
label2 = (TextView)findViewById(R.id.label2);
textView = (TextView) findViewById(R.id.label);
int permissionStatus = ContextCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION);
if (permissionStatus == PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),"access is",Toast.LENGTH_LONG).show();
} else {
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.ACCESS_FINE_LOCATION},
1);
}
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000 * 10,
10, this);
queue = Volley.newRequestQueue(this);
}
@Override
public void onLocationChanged(final Location location) {
textView.setText("My location = Latitude:" + location.getLatitude() + ", Longitude:" + location.getLongitude());
if (latitude != location.getLatitude() && longitude != location.getLongitude()) {
Toast.makeText(this,"location changed",Toast.LENGTH_LONG).show();
latitude = location.getLatitude();
longitude = location.getLongitude();
getvalue();
}
}
@Override
public void onProviderDisabled(String provider) {
Log.e("Latitude","disable");
}
@Override
public void onProviderEnabled(String provider) {
Log.e("Latitude","enable");
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
Log.e("Latitude","status");
}
public void Insert(View view){
name = editText.getText().toString();
Insert.BackGround b=new Insert.BackGround();
String lan = String.valueOf(latitude);
String lon = String.valueOf(longitude);
b.execute(name,lan,lon);
String result = b.Result();
Toast.makeText(this,result,Toast.LENGTH_LONG).show();
}
public void getvalue(){
Log.e("st","getvalue");
String url = "http://my_url";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String name = response.getString("name");
Lan = response.getDouble("latitude");
Lon = response.getDouble("longitude");
Log.e("lan", String.valueOf(Lan));
Log.e("lon", String.valueOf(Lon));
calculate(latitude,longitude,Lan,Lon);
label3.setText("name="+name+" Lan="+Lan+" Lon="+Lon);
} catch (JSONException ex) {
ex.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
queue.add(request);
}
public void calculate( double lan1, double lon1, double lan2, double lon2){
Log.e("st","calculate");
Location loc1 = new Location("point A");
loc1.setLatitude(lan1);
loc1.setLongitude(lon1);
Location loc2 = new Location("point B");
loc2.setLatitude(lan2);
loc2.setLongitude(lon2);
String distanceInMeters = String.valueOf(loc1.distanceTo(loc2));
label2.setText("Distance = "+distanceInMeters );
}
显现:
<uses-sdk
android:targetSdkVersion="28" />
<uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission. ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme"
android:networkSecurityConfig="@xml/network_security_config"
android:usesCleartextTraffic="true"
android:testOnly="false"
android:debuggable="true"
tools:ignore="HardcodedDebugMode">
该程序在 Redmi Note 4 和模拟器 BlueStacks 中运行良好,但在 Redmi Note 5 和 Android > 8 应用程序中出现此类错误“应用程序不断停止”
我在互联网上找不到错误和答案,请帮助我
如果有人编写了可以在任何设备上运行的代码,我会很高兴:)
安卓日志猫
2020-02-26 18:22:42.553 3897-3897/com.example.getlocation E/GMPM: GoogleService failed to initialize, status: 10, Missing an expected resource: 'R.string.google_app_id' for initializing Google services. Possible causes are missing google-services.json or com.google.gms.google-services gradle plugin.
2020-02-26 18:22:42.554 3897-3897/com.example.getlocation E/GMPM: Scheduler not set. Not logging error/warn.
2020-02-26 18:22:42.560 3897-3927/com.example.getlocation E/GMPM: Uploading is not possible. App measurement disabled
2020-02-26 18:22:44.854 3897-3897/com.example.getlocation E/st: getvalue
2020-02-26 18:22:44.874 3897-3931/com.example.getlocation E/AndroidRuntime: FATAL EXCEPTION: Thread-4
Process: com.example.getlocation, PID: 3897
java.lang.NoClassDefFoundError: Failed resolution of: Lorg/apache/http/ProtocolVersion;
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:108)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.ProtocolVersion" on path: DexPathList[[zip file "/data/app/com.example.getlocation-9UATZ0srGfq2w1rY4vtYKQ==/base.apk"],nativeLibraryDirectories=[/data/app/com.example.getlocation-9UATZ0srGfq2w1rY4vtYKQ==/lib/arm64, /system/lib64]]
at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:134)
at java.lang.ClassLoader.loadClass(ClassLoader.java:379)
at java.lang.ClassLoader.loadClass(ClassLoader.java:312)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:108)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:93)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:105)
您可能正在使用已弃用的库进行截击。 改用正式版
实现 'com.android.volley:volley:1.1.1'
对于我在日志中看到的其他问题 cat 与此GoogleService 初始化失败有关,状态:10,缺少预期资源:用于初始化 Google 服务的“R.string.google_app_id”。 可能的原因是缺少 google-services.json 或 com.google.gms.google-services gradle 插件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.