Failcz.msebera.android.httpclient.client.HttpResponseException:为天气应用程序发出HTTP请求时出现错误请求错误

时间:2018-02-11 22:04:58

标签: java android json loopj

我正在使用' com.loopj.android:android-async-http:1.4.9'库并尝试从以下URL http://api.openweathermap.org/data/2.5/weather接收天气数据JSON对象 由于安全原因,API密钥被隐藏。

公共类WeatherController扩展了AppCompatActivity {

console.log("test");

// TODO:在这里添加letsDoSomeNetworking(RequestParams params):

// Constants:
final int REQUEST_CODE = 123;

final String WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather";
// App ID to use OpenWeather data
final String APP_ID = "e****************************a";
// 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;


// Member Variables:
TextView mCityLabel;
ImageView mWeatherImage;
TextView mTemperatureLabel;

// TODO: Declare a LocationManager and a LocationListener here:
LocationManager mLocationManager; // start or stop requesting location updates
LocationListener mLocationListener;// it will be notified is the location is actually changed


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

    // Linking the elements in the layout to Java code
    mCityLabel = (TextView) findViewById(R.id.locationTV);
    mWeatherImage = (ImageView) findViewById(R.id.weatherSymbolIV);
    mTemperatureLabel = (TextView) findViewById(R.id.tempTV);
    ImageButton changeCityButton = (ImageButton) findViewById(R.id.changeCityButton);


    // TODO: Add an OnClickListener to the changeCityButton here:

}


// TODO: Add onResume() here:
@Override
protected void onResume() {
    super.onResume();
    Log.d("clima", "onResume() called");
    Log.d("clima", "gettin weather for current location");
    getWeatherForCurrentLocation();


}


// TODO: Add getWeatherForNewCity(String city) here:


// TODO: Add getWeatherForCurrentLocation() here:

private void getWeatherForCurrentLocation() {

    mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);

    mLocationListener = new LocationListener() {
        @Override
        public void onLocationChanged(Location location) {
            Log.d("clima", "onLocationChnaged() callback recieved");
           String longitude = String.valueOf(location.getLongitude());
           String latitude = String.valueOf(location.getLatitude());

           Log.d("clima","longitude is"+longitude);

            Log.d("clima","latitude is"+latitude);

            RequestParams params = new RequestParams();
            params.put("lat",latitude);
            params.put(",lon",longitude);
            params.put("appid",APP_ID);
            letsDoSomeNetworking(params);

        }

        @Override
        public void onStatusChanged(String provider, int status, Bundle extras) {

        }

        @Override
        public void onProviderEnabled(String provider) {

        }

        @Override
        public void onProviderDisabled(String provider) {

            Log.d("clima", "onProviderDisabled() callback recievd");


        }
    };
    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) {

            Log.d("clima", "onRequestPermissionsResult() : Permission granted");
            getWeatherForCurrentLocation();

        } else {

            Log.d("clima", "permission denied");

        }


    }


}

Trying to acess the JSON Object and getting msebera.android.httpclient.client.HttpResponseException: Bad Request and status as code400, which is for unsuccessful request attempt

2 个答案:

答案 0 :(得分:0)

修正这两行代码

final String WEATHER_URL = "http://api.openweathermap.org/data/2.5/weather";    
final String APP_ID = "e****************************a";

在第1行,您在API调用的基本网址后面没有任何内容,您需要附加:

?lat=-122.5564&lon=37.1516&appid=e****************************a

并且您将在开放天气中注册以获取您的应用ID,因为您正在构建天气应用

第二行放了APP_ID

答案 1 :(得分:0)

您的代码:

params.put(",lon",longitude);

看起来像拼写错误,因为括号内有两个逗号,删除引号内的第一个逗号。