In my app, I'm trying to connect with API using hashmap from a fragment. The issue seems to be that the async task through which I'm connecting to the API doesn't want to accept my request. The error says Expected URL scheme 'http' or 'https' but no colon was found
It is an app, where I use spinner so a user can choose, depending on his choice, different data will be loaded. I tried to fiddle around in hash map, but the issue now seems to be in the creating task.
this is my spinner where I initiate the task
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id){
Toast.makeText(getActivity(),"Location selected: "+locations[position], Toast.LENGTH_SHORT )
.show();
map.get(locations[position]);
//create the task
ChooseLocationTask task = new ChooseLocationTask(location, position, value);
// start the task
task.execute();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
and here is the constructor for the async task, that seems to be not working as well:
this.location = location;
this.value = value;
String url ="";
switch(selected){
case 1:
url = url1.getUrl();
break;
case 2:
url= url2.getUrl();
break;
case 3:
url= url3.getUrl();
break;
case 4:
url= url4.getUrl();
break;
case 5:
url = url5.getUrl();
break;
case 6:
url = url6.getUrl();
break;
case 7:
url = url7.getUrl();
break;
case 8:
url= url8.getUrl();
break;
case 9:
url = url9.getUrl();
}
request = new Request.Builder().url(url).build();
}
The error in the logcat said that the issue is in creating the task in the first code example and request in the second example. I would like the user to see values from API when clicking on some of the fields in the spinner. Thanks for your help
URL must start with http:// or https://, your url is ""
EDIT: another possible problem that I see is that you don't have a default: branch in your switch statement - if variable selected has value of less than 1 or more than 9 your url will be empty and thus will not start with http:// or https://
Also it seems you never use the variable called value. Maybe you meant to use that in your switch statement ?
In my case I forgot to add "base URL" in retrofit creation.So,even in this case it throws error.
Error:
return Retrofit.Builder()
.client(okHttpClient)
.addConverterFactory(gsonConverterFactory)
.baseUrl(" ") //Here I had missed url
.build()
Correct:
return Retrofit.Builder()
.client(okHttpClient)
.addConverterFactory(gsonConverterFactory)
.baseUrl(BASE_URL) //Add URL in place of base url
.build()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With