简体   繁体   中英

How can i fix URI build error

In android a have a URL like http://api.openweathermap.org/data/2.5/forecast/daily?q=khulna&mode=json&units=metric&cnt=7&appid=02b7 ;

So I use Uri builder

final String FORECAST_BASE_URL=
            "http://api.openweathermap.org/data/2.5/forecast/daily?";
    final String QUERY_PARAM="q";
    final String FORMAT_PARAM="mode";
    final String UNITS_PARAM = "units";
    final String DAYS_PARAM="cnt";
    final String appID="&appid=";


    //http://api.openweathermap.org/data/2.5/forecast/daily?q=khulna&mode=json&units=metric&cnt=7


    //now uri build
    Uri buildUri =  Uri.parse(FORECAST_BASE_URL).buildUpon()
            .appendQueryParameter(QUERY_PARAM,"khulna")
            .appendQueryParameter(FORMAT_PARAM,"json")
            .appendQueryParameter(UNITS_PARAM,"metric")
            .appendQueryParameter(DAYS_PARAM,Integer.toString(7))
            .appendQueryParameter(appID, BuildConfig.OPEN_WEATHER_MAP_API_KEY)
            .build();

But the output is http://api.openweathermap.org/data/2.5/forecast/daily?q=khulna&mode=json&units=metric&cnt=7&%26appid%3D=02b7

here two extra characters '%26' and '%3D';

I convert this URI to string and replace those two characters by using replace method

String url = buildUri.toString().replace("%3D","").replace("%26","");

But my question is How can I build that expected Uri not replacing two characters through String replace methods.

change:

final String appID="&appid=";

to

final String appID="appid";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM