我有以下代码通过volley
库将我的手机位置发送到服务器,它只能通过模拟器运行,但不能通过任何手机运行。
我无法再调查错误,我试图更改minSdk
但这没有任何结果。
MinSdk:17
目标:Android Nougat Api 25(7.1.1)
public class MainActivity extends AppCompatActivity {
private Button start, stop;
private TextView t;
private LocationManager locationManager;
private LocationListener listener;
private String url = "https://vamos.comeze.com/api/tracking";
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
t = (TextView) findViewById(R.id.textView);
locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
listener = new LocationListener() {
@Override
public void onLocationChanged(final Location location) {
t.setText("\n " + location.getLongitude() + " " + location.getLatitude());
StringRequest stringRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "Error Response", Toast.LENGTH_SHORT).show();
Log.e("Hellllllllo", "Error Response");
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("longitude", location.getLongitude() + "");
params.put("latitude", location.getLatitude() + "");
Log.e("Longitude", location.getLongitude() + "");
Log.e("Latitude", location.getLatitude() + "");
return params;
}
};
MySingleton.getInstance(MainActivity.this).addTorequestque(stringRequest);
}
@Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
@Override
public void onProviderEnabled(String s) {
}
@Override
public void onProviderDisabled(String s) {
Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
};
start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Start", "Yes on Start");
configure_button();
}
});
stop.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("Stop", "Yes on Stop");
locationManager.removeUpdates(listener);
}
});
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case 10:
configure_button();
break;
default:
break;
}
}
void configure_button() {
// first check for permissions
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
requestPermissions(new String[]{Manifest.permission.ACCESS_COARSE_LOCATION, Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.INTERNET}
, 10);
}
return;
}
locationManager.requestLocationUpdates("gps", 5000, 1, listener);
}
}