2017-07-29 60 views
0

我想建立在这里我使用JSON的应用程序来获取当前latitudelongitude,也是address从它,但只要我点击button我得到一个错误在我的日志:BasicNetwork.performRequest:意外的响应代码400的Android

E /排球:[5807] BasicNetwork.performRequest:意外的响应码400,用于https://maps.googleapis.com/maps/api/geocode/json?latlng=0.00.0

的代码如下给出:

Gps3Activity.java

public class Gps3Activity extends AppCompatActivity { 
    private Button display; 
    private LocationManager locationManager; 
    private LocationListener locationListener; 
    private RequestQueue requestQueue; 
    private double lat; 
    private double lng; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_gps3); 

     display = (Button) findViewById(R.id.displayL); 
     requestQueue = Volley.newRequestQueue(this); 

     locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
     locationListener = new myLocationlistener(); 
     if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

      return; 
     } 
     locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener); 

     display.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 

       Log.e("Latitude", String.valueOf(lat)); 
       Log.e("Longitude", String.valueOf(lng)); 

       JsonObjectRequest request = new JsonObjectRequest("http://maps.googleapis.com/maps/api/geocode/json?latlng="+ lat+","+lng +"&sensor=true", new Response.Listener<JSONObject>() { 

        @Override 
        public void onResponse(JSONObject response) { 
         try { 
          String address = response.getJSONArray("results").getJSONObject(0).getString("formatted_address"); 
          //textViewCity.setText(address); 
          Toast.makeText(getApplicationContext(), "City : " + address, Toast.LENGTH_LONG); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } 
        } 
       }, new Response.ErrorListener() { 
        @Override 
        public void onErrorResponse(VolleyError error) { 

        } 
       }); 
       requestQueue.add(request); 
      } 
     }); 
    } 

    private class myLocationlistener implements LocationListener { 
     @Override 
     public void onLocationChanged(Location location) { 
      if (location != null) { 
       lat = location.getLatitude(); 
       lng = location.getLongitude(); 
      } 
     } 

     @Override 
     public void onStatusChanged(String s, int i, Bundle bundle) { 

     } 

     @Override 
     public void onProviderEnabled(String s) { 

     } 

     @Override 
     public void onProviderDisabled(String s) { 

     } 
    } 
} 

logcat的

07-30 01:18:33.287 9750-9750/com.example.shaloin.gps2 W/System.err: org.json.JSONException: Index 0 out of range [0..0) 
07-30 01:18:33.288 9750-9750/com.example.shaloin.gps2 W/System.err:  at org.json.JSONArray.get(JSONArray.java:293) 
07-30 01:18:33.288 9750-9750/com.example.shaloin.gps2 W/System.err:  at org.json.JSONArray.getJSONObject(JSONArray.java:521) 
07-30 01:18:33.288 9750-9750/com.example.shaloin.gps2 W/System.err:  at com.example.shaloin.gps2.Gps3Activity$1$1.onResponse(Gps3Activity.java:63) 
07-30 01:18:33.288 9750-9750/com.example.shaloin.gps2 W/System.err:  at com.example.shaloin.gps2.Gps3Activity$1$1.onResponse(Gps3Activity.java:58) 

而且我无法弄清楚,为什么我收到的latitudelongitude值作为0.0

任何人都可以帮忙吗?谢谢:)

回答

0

我只好改变了JsonObjectRequest()的格式。正确的格式在下面给出由@ZeekHuge

JsonObjectRequest(int method, 
       String url, 
       JSONObject jsonRequest, 
       Response.Listener<JSONObject> listener, 
       Response.ErrorListener errorListener) 

所建议下面的正确的代码给出:

public class Gps3Activity extends AppCompatActivity { 

private static final int MY_PERMISSIONS_REQUEST_SEND_SMS =0 ; 
private Button display; 
private TextView displayLocation; 
private LocationManager locationManager; 
private LocationListener locationListener; 
private RequestQueue requestQueue; 
private double lat; 
private double lng; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_gps3); 

    display = (Button) findViewById(R.id.displayL); 
    displayLocation=(TextView)findViewById(R.id.location1); 
    requestQueue = Volley.newRequestQueue(this); 

    locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); 
    locationListener = new myLocationlistener(); 
    if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) != PackageManager.PERMISSION_GRANTED) { 

     return; 
    } 
    locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 0, locationListener); 

    display.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      getLocation(lat,lng); 

      Log.e("Latitude", String.valueOf(lat)); 
      Log.e("Longitude", String.valueOf(lng)); 
      Log.e("city",address); 
      Toast.makeText(getApplicationContext(), "City : " + address, Toast.LENGTH_LONG); 
      displayLocation.setText("City : "+address); 
} 
    }); 
} 

private class myLocationlistener implements LocationListener { 
    @Override 
    public void onLocationChanged(Location location) { 
     if (location != null) { 
      lat = location.getLatitude(); 
      lng = location.getLongitude(); 
      //Toast.makeText(getApplicationContext(),"Latitude: "+lat+"\nLongitude: "+lng,Toast.LENGTH_LONG).show(); 
      getLocation(lat,lng); 
     } 
    } 

    @Override 
    public void onStatusChanged(String s, int i, Bundle bundle) { 

    } 

    @Override 
    public void onProviderEnabled(String s) { 

    } 

    @Override 
    public void onProviderDisabled(String s) { 

    } 
} 

public void getLocation(double latitude,double longitude){ 
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, 
      "http://maps.googleapis.com/maps/api/geocode/json?latlng="+ latitude+","+longitude +"&sensor=true", 
      new Response.Listener<JSONObject>() { 

       @Override 
       public void onResponse(JSONObject response) { 
        try { 
         address = response.getJSONArray("results").getJSONObject(0).getString("formatted_address"); 
         //textViewCity.setText(address); 
         //Toast.makeText(getApplicationContext(), "City : " + address, Toast.LENGTH_LONG); 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
       } 
      }, new Response.ErrorListener() { 
     @Override 
     public void onErrorResponse(VolleyError error) { 

     } 
    }); 
    requestQueue.add(request); 
} 
} 

尽管通过使用这种方法需要花费大量的时间来显示location

0

似乎有3个问题有:

第一:

JsonObjectRequest()的格式似乎是完全错误的。正确的格式为:

JsonObjectRequest(int method, 
        String url, 
        JSONObject jsonRequest, 
        Response.Listener<JSONObject> listener, 
        Response.ErrorListener errorListener) 

参考thisthis

二:你都可以从地理编码API意外的响应

使用谷歌的地理编码的API,你必须首先让自己的API密钥。 格式应该是这样的:(指this文件)

https://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&location_type=ROOFTOP&result_type=street_address&key=YOUR_API_KEY 

所以你可以看到,它需要的API密钥。您可以阅读here以获取密钥。另外注意表示纬度值和经度值之间有逗号(,)。你的代码缺少。

第三:你得到latlng为0.0

这是因为位置数据需要时间来获取。操作系统不会调用onLocationChange(),直到它具有一定的精度的位置数据。所以,当你点击按钮时,你到那时还没有更新位置。你应该做的是,当用户点击按钮时,向他展示一个消息为updating location的进度条,并且只有在调用onLocationChange()时才解除它。仅当'onLocationChange()'被调用至少一次时,才使用latlng

+0

我一直在使用https://stackoverflow.com/questions/20238197/get-current-location-using-json/20238474#20238474制作应用程序时,正如你所说的关于api键,接受的答案在该职位没有它。 –

+0

如果你想按照3岁的答案去选择,那就选择你。用户可能认为它是“隐含的”,或者相信这些[google文档](https://developers.google.com/maps/documentation/geocoding/intro),这些API的所有者 – ZeekHuge

+0

好吧,那么正如你所说,我错过了昏迷,我解决了这个问题,现在日志显示如下:'W/System.err:org.json.JSONException:索引0超出范围[0..0]' –

相关问题