Android 获取数据-如何?

Android 获取数据-如何?,android,Android,我正在尝试根据设备坐标从api获取数据。 我正在通过模拟器中的外部控件传递纬度和经度。我正在尝试根据用户的输入显示属性详细信息。提前感谢您的帮助。 但是我得到的是状态代码403 错误消息: Failcz.msebera.android.httpclient.client.HttpResponseException: 禁止的 代码如下: public class WeatherController extends AppCompatActivity { // Constants:

我正在尝试根据设备坐标从api获取数据。 我正在通过模拟器中的外部控件传递纬度和经度。我正在尝试根据用户的输入显示属性详细信息。提前感谢您的帮助。 但是我得到的是状态代码
403

错误消息:

Failcz.msebera.android.httpclient.client.HttpResponseException: 禁止的

代码如下:

public class WeatherController extends AppCompatActivity {

    // Constants:
    final int REQUEST_CODE=123;
    final String WEATHER_URL_1 = "https://search.onboard-apis.com/propertyapi/v1.0.0/property/snapshot";

    final String APP_ID_1 = "e6cf3a491f0fc65838fe6b9e";
    // Time between location updates (5000 milliseconds or 5 seconds)
    final long MIN_TIME = 5000;
    // Distance between location updates (1000m or 1km)
    final float MIN_DISTANCE = 1000;

    // TODO: Set LOCATION_PROVIDER here:
    String LOCATION_PROVIDER = LocationManager.GPS_PROVIDER;




    // TODO: Declare a LocationManager and a LocationListener here:

    LocationManager mLocationManager;
    LocationListener mLocationListener;




    private void getWeatherForCurrentLocation() {
        mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        mLocationListener = new LocationListener() {
            @Override
            public void onLocationChanged(Location location) {
                Log.d("Clima", "OnLocationChanged callback received");
                String longitudes=String.valueOf(location.getLongitude());
                String latitudes=String.valueOf(location.getLatitude());
                RequestParams params=new RequestParams();
                params.put("latitude",latitudes);
                params.put("longitude",longitudes);
                params.put("appid",APP_ID_1);
                letsDoSomeMetworking(params);

            }

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

            }

            @Override
            public void onProviderEnabled(String s) {

            }

            @Override
            public void onProviderDisabled(String s) {
                Log.d("Clima", "OnProviderDisabled called");
            }
        };
        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, 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.
            ActivityCompat.requestPermissions(this,new String[]{Manifest.permission.ACCESS_FINE_LOCATION},REQUEST_CODE);
            return;
        }
        mLocationManager.requestLocationUpdates(LOCATION_PROVIDER, MIN_TIME, MIN_DISTANCE, mLocationListener);
        }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        if(requestCode==REQUEST_CODE)
        {
            if(grantResults.length>0&&grantResults[0]==PackageManager.PERMISSION_GRANTED);
            getWeatherForCurrentLocation();
        }
        else
        {
            Log.d("Clima","Permission denied");
        }
    }
// TODO: Add letsDoSomeNetworking(RequestParams params) here:

    private void letsDoSomeMetworking(RequestParams params)
    {
        AsyncHttpClient client=new AsyncHttpClient();
        client.get(WEATHER_URL_1,params,new JsonHttpResponseHandler(){
        @Override
            public void onSuccess(int statusCode, Header[] headers, JSONObject response)
        {
            Log.d("Clima","Success JSON::"+response.toString());
        }
        @Override
            public void onFailure(int statusCode,Header[] headers,Throwable e,JSONObject response)
        {
            Log.e("Clima","Fail"+e.toString());
            Log.d("Clima","Status code:"+statusCode);
        }
        });
    }

根据,您需要将API密钥作为头发送。我在你的代码中没有看到这一点。我猜这就是服务器拒绝您的请求的原因。

您的“if(grantResults.length>0&&…”行后面是否应该有一个分号?我想这表示,“if grantResults.length>0&&…什么都不做。现在,始终调用getWeatherForCurrentLocation(),即使grantResults.length为0。您可能应该考虑添加{}”或者至少去掉if语句的分号。仍然得到相同的错误,但谢谢。请告诉我如何将其作为标头传递?我不熟悉AsyncHttpClient库,但似乎您可以只执行
client。addHeader(key,value)
解决了它,感谢您为我指明了正确的方向