获取数据 - 如何?

时间:2017-12-12 16:45:50

标签: android

我正在尝试根据设备的坐标从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);
        }
        });
    }

1 个答案:

答案 0 :(得分:1)

根据Onboard API documentation,您需要将API密钥作为标头发送。我在你的代码中没有看到这个。我猜这就是为什么你的请求被服务器拒绝了。