Java 截击失误
我想使用从开放的街道地图API 当我发送请求以截取方式反转地理编码Api时 将显示此错误 com.android.volley.authfailureerror 但是这个API不需要任何标记 这是我的代码:Java 截击失误,java,android,api,android-volley,Java,Android,Api,Android Volley,我想使用从开放的街道地图API 当我发送请求以截取方式反转地理编码Api时 将显示此错误 com.android.volley.authfailureerror 但是这个API不需要任何标记 这是我的代码: String url = "https://nominatim.openstreetmap.org/reverse?format=json&lat="+lat+"&lon="+lng; Response.Listener<JSONObject> list
String url = "https://nominatim.openstreetmap.org/reverse?format=json&lat="+lat+"&lon="+lng;
Response.Listener<JSONObject> listener = new Response.Listener<JSONObject>()
{
@Override
public void onResponse(JSONObject response)
{
try
{
JSONObject address = response.getJSONObject("address");
final String city = address.getString("town");
final String county = address.getString("county");
selectcity = true;
String temp = county.substring(county.indexOf(" "),county.length());
ed.putString("lat", lat+"");
ed.putString("lng", lng+"");
ed.putString("location", temp);
ed.apply();
progressDialog.dismiss();
RequestQueue mRequestQueue;
mRequestQueue =AppController.getInstance().getRequestQueue();
mRequestQueue.cancelAll("AppController");
locationManager.removeUpdates(LocationActivity.this);
}
catch (JSONException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Erorr", Toast.LENGTH_LONG).show();
RequestQueue mRequestQueue;
mRequestQueue =AppController.getInstance().getRequestQueue();
mRequestQueue.cancelAll("AppController");
progressDialog.dismiss();
locationManager.removeUpdates(LocationActivity.this);
}
}
};
Response.ErrorListener errorListener = new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
RequestQueue mRequestQueue;
mRequestQueue =AppController.getInstance().getRequestQueue();
mRequestQueue.cancelAll("AppController");
progressDialog.dismiss();
locationManager.removeUpdates(LocationActivity.this);
}
};
final JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null, listener, errorListener){
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
headers.put("accept-language", "fa");
return headers;
}
};
request.setTag("AppController");
AppController.getInstance().addToRequestQueue(request);
stringurl=”https://nominatim.openstreetmap.org/reverse?format=json&lat=“+lat+”&lon=“+lng;
Response.Listener=新建Response.Listener()
{
@凌驾
公共void onResponse(JSONObject响应)
{
尝试
{
JSONObject地址=response.getJSONObject(“地址”);
最终字符串city=address.getString(“town”);
最终字符串country=address.getString(“country”);
选择city=true;
字符串temp=country.substring(country.indexOf(“”,country.length());
ed.putString(“lat”,lat+);
ed.putString(“lng”,lng+);
ed.putString(“位置”,温度);
d.应用();
progressDialog.disclose();
请求队列mRequestQueue;
mRequestQueue=AppController.getInstance().getRequestQueue();
mRequestQueue.cancelAll(“AppController”);
locationManager.RemoveUpdate(LocationActivity.this);
}
捕获(JSONException e)
{
e、 printStackTrace();
Toast.makeText(getApplicationContext(),“Erorr”,Toast.LENGTH_LONG.show();
请求队列mRequestQueue;
mRequestQueue=AppController.getInstance().getRequestQueue();
mRequestQueue.cancelAll(“AppController”);
progressDialog.disclose();
locationManager.RemoveUpdate(LocationActivity.this);
}
}
};
Response.ErrorListener ErrorListener=新的Response.ErrorListener()
{
@凌驾
公共无效onErrorResponse(截击错误)
{
Toast.makeText(getApplicationContext(),“Error”,Toast.LENGTH_LONG.show();
请求队列mRequestQueue;
mRequestQueue=AppController.getInstance().getRequestQueue();
mRequestQueue.cancelAll(“AppController”);
progressDialog.disclose();
locationManager.RemoveUpdate(LocationActivity.this);
}
};
final JsonObjectRequest=新JsonObjectRequest(request.Method.GET、url、null、侦听器、errorListener){
@凌驾
公共映射getHeaders()引发AuthFailureError{
标题。put(“接受语言”、“fa”);
返回标题;
}
};
request.setTag(“AppController”);
AppController.getInstance().addToRequestQueue(请求);
此外,当我在《邮差》中使用此URL时,它可以正常工作
RequestQueue requestQueue = Volley.newRequestQueue(mContext);
StringRequest stringRequest = new StringRequest(Request.Method.GET, Your_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.d("===","onErrorResponse "+error)
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
Log.d("===","AuthFailureError");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
try {
} catch (Exception e) {
}
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
stringRequest.setRetryPolicy(new RetryPolicy() {
@Override
public int getCurrentTimeout() {
return 50000;
}
@Override
public int getCurrentRetryCount() {
return 50000;
}
@Override
public void retry(VolleyError error) throws VolleyError {
}
});
requestQueue.add(stringRequest);
RequestQueue-RequestQueue=Volley.newRequestQueue(mContext);
StringRequest StringRequest=newStringRequest(Request.Method.GET,您的URL,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
//回应
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.d(“=”,“OneErrorResponse”+错误)
}
}) {
@凌驾
公共字符串getBodyContentType(){
返回“application/json;charset=utf-8”;
}
@凌驾
公共字节[]getBody()抛出AuthFailureError{
试一试{
Log.d(“==”,“AuthFailureError”);
}捕获(不支持的编码异常uee){
wtf(“尝试使用%s获取%s的字节时不支持编码”,请求体,“utf-8”);
返回null;
}
}
@凌驾
公共映射getHeaders()引发AuthFailureError{
Map params=新的HashMap();
参数put(“内容类型”、“应用程序/json”);
返回参数;
}
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应){
字符串responseString=“”;
if(响应!=null){
responseString=String.valueOf(response.statusCode);
//可以获取更多详细信息,如response.headers
试一试{
}捕获(例外e){
}
}
返回Response.success(responseString,HttpHeaderParser.parseCacheHeaders(Response));
}
};
stringRequest.setRetryPolicy(新RetryPolicy(){
@凌驾
public int getCurrentTimeout(){
返回50000;
}
@凌驾
public int getCurrentRetryCount(){
返回50000;
}
@凌驾
公共无效重试(截击错误)引发截击错误{
}
});
添加(stringRequest);
创建一个“尝试但不工作”的解决方案我使用改装来解决我的问题