问题:点击搜索后,标记不再显示,并向我提供(链接)的错误响应代码400
logcat错误
E/Response:: [{"lat":"-7.552991","lng":"110.798546","id":"13","nama":"SMK 4 NEGERI SURAKARTA"}]
E/Volley: [4208] BasicNetwork.performRequest: Unexpected response code 400 for http://tugasupload.esy.es/api/maps.php?latitude=SMK 4 NEGERI SURAKARTA
和源代码
private void getMarkers() {
System.out.println("abc");
dialog.setMessage("Mengambil Data");
dialog.setCancelable(false);
dialog.show();
JsonArrayRequest reqData = new JsonArrayRequest(Request.Method.GET,
"http://tugasupload.esy.es/api/maps.php?latitude=" + ambilcari, null,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
for (int i = 0; i < response.length(); i++) {
try {
JSONObject data = response.getJSONObject(i);
title = data.getString("nama");
id = data.getInt("id");
latLng = new LatLng(Double.parseDouble(data.getString(LAT)), Double.parseDouble(data.getString(LNG)));
lokasi = data.getString("nama");
// Menambah data marker untuk di tampilkan ke google map
addMarker(latLng, title, lokasi, id);
} catch (JSONException e) {
e.printStackTrace();
}
}
dialog.cancel();
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
dialog.cancel();
Toast.makeText(getApplication(), "Ada Kesalahan Mohon Periksa Kembali", Toast.LENGTH_LONG).show();
}
});
AppController.getInstance().addToRequestQueue(reqData);
}
@Override
public void onClick(View view) {
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
assert imm != null;
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
ambilcari = acTextView.getText().toString();
lokasi();
gMap.clear();
}
private void lokasi() {
dialog.setMessage("Loading...");
dialog.show();
String url1 = "http://tugasupload.esy.es/api/lokasi.php?latitude=" + ambilcari;
url1= url1.replaceAll(" ", "%20");
System.out.println(url1);
StringRequest strReq = new StringRequest(Request.Method.GET, url1, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.e("Response: ", response);
try {
JSONArray jArray = new JSONArray(response);
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
System.out.println(latLng);
lati = Double.parseDouble(jsonObject.getString(LAT));
longi = Double.parseDouble(jsonObject.getString(LNG));
if (ambilcari.equals(" ")) {
center = new LatLng(-7.568737, 110.827062);
cameraPosition = new CameraPosition.Builder().target(center).zoom(10).build();
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
getMarkers();
} else {
center = new LatLng(lati, longi);
cameraPosition = new CameraPosition.Builder().target(center).zoom(12).build();
gMap.animateCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
getMarkers();
}
}
dialog.dismiss();
} catch (JSONException e) {
// JSON error
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse( VolleyError volleyError ) {
try {
String responseBody = new String( volleyError.networkResponse.data, "utf-8" );
JSONObject jsonObject = new JSONObject( responseBody );
} catch ( JSONException e ) {
//Handle a malformed json response
} catch (UnsupportedEncodingException ignored){
}
}
}
);
AppController.getInstance().addToRequestQueue(strReq, tag_json_obj);
}
,我正在尝试通过以下方法解决此问题: Android Volley - BasicNetwork.performRequest: Unexpected response code 400
并在搜索后添加此代码之前添加url = url.replaceAll(" ", "%20");
,强制关闭应用程序