有谁知道为什么凌空重复它的反应?香港专业教育学院已花了2个小时搞清楚为什么会这样。我发送重复的请求?或者在1个请求中向我发送重复数据?
SwipeRefreshLayout swipe;
ListView list;
CustomListAdapter adapter;
List <Distance> itemList = new ArrayList <>();
Double latitude, longitude;
Criteria criteria;
Location location;
LocationManager locationManager;
String provider;
private static final String url = "https://phagedenic-roots.000webhostapp.com/thesis/haversine.php?latt=";
public ListViewFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_list_view, container, false);
list = (ListView) view.findViewById(R.id.list);
swipe = (SwipeRefreshLayout) view.findViewById(R.id.swipe);
// mengisi data dari adapter ke listview
adapter = new CustomListAdapter(getActivity(), itemList);
list.setAdapter(adapter);
locationManager = (LocationManager) getActivity().getSystemService(Context.LOCATION_SERVICE);
criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
swipe.setOnRefreshListener(this);
swipe.post(new Runnable() {
@Override
public void run() {
location();
}
});
return view;
}
@Override
public void onRefresh() {
location();
}
private void location() {
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
location = locationManager.getLastKnownLocation(provider);
if (ActivityCompat.checkSelfPermission(getActivity(), Manifest.permission.ACCESS_FINE_LOCATION) !=
PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(getActivity(),
Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) {
// TODO: Consider calling
// ActivityCompat#requestPermissions
// here to request the missing permissions, and then overriding
// public void onRequestPermissionsResult(int requestCode, String[] permissions,
// int[] grantResults)
// to handle the case where the user grants the permission. See the documentation
// for ActivityCompat#requestPermissions for more details.
return;
}
// request location update of device within per second
locationManager.requestLocationUpdates(provider, 1000, 1, (LocationListener) this);
if (location != null) {
onLocationChanged(location);
} else {
/* latitude longitude Alun-alun Demak sebagai default jika tidak ditemukan lokasi dari device pengguna */
callListVolley(13.138075, 123.735694);
}
}
private void callListVolley(double lat, double lng) {
itemList.clear();
adapter.notifyDataSetChanged();
swipe.setRefreshing(true);
JsonArrayRequest jArr = new JsonArrayRequest(url + lat + "&longt=" + lng,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.e(TAG, response.toString());
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
Distance j = new Distance();
j.setApartmentname(obj.getString("apartmentName"));
j.setPrice(obj.getString("price_month"));
j.setCategory(obj.getString("Category"));
double distance = Double.parseDouble(obj.getString("distance"));
j.setDistance("" + round(distance, 2));
itemList.add(j);
} catch (JSONException e) {
e.printStackTrace();
}
}
// memberitahu adapter jika ada perubahan data
adapter.notifyDataSetChanged();
swipe.setRefreshing(false);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e(TAG, "Error: " + error.getMessage());
Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_LONG).show();
swipe.setRefreshing(false);
}
});
// menambah permintaan ke queue
AppController.getInstance().addToRequestQueue(jArr);
}
public void onBackPressed() {
getActivity().finish();
System.exit(0);
}
// to get the shortest distance
public static double round(double value, int places) {
if (places < 0) throw new IllegalArgumentException();
long factor = (long) Math.pow(10, places);
value = value * factor;
long tmp = Math.round(value);
return (double) tmp / factor;
}
// location of the current user
@Override
public void onLocationChanged(Location location) {
latitude = location.getLatitude();
longitude = location.getLongitude();
Log.e(TAG, "User location latitude:" + latitude + ", longitude:" + longitude);
callListVolley(latitude, longitude);
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
}
这是logcat的响应。我花了2个小时才弄明白它的错误是什么:(请帮忙。
答案 0 :(得分:0)
你的排球请求工作正常。
您只需输入两次onLocationChanged(Location location)
,这意味着您的位置提供商会向您发送最后一个已知位置,并且可能会立即发送更新。
我怀疑您的问题来自location()
中的onResume()
。
您应该设置LocationManager
并在requestLocationUpdates
中请求onCreate()
,并仅在onResume()
中保留权限管理部分。这只是我的猜测,我没有尝试修改你的代码,因为我现在没有足够的时间.`