D. Fadare D. Fadare - 4 months ago 59
Android Question

Using IOUtils in Android to convert Uri/Url to String

I've researched and created the following code in my android studio project

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Utils.onActivityCreateSetTheme(this);
setContentView(R.layout.activity_main);
TextView notificationsText = (TextView) findViewById(R.id.notifications_text);
Uri one = Uri.parse("http://textfiles.com/adventure/221baker.txt");
Uri two = Uri.parse("http://textfiles.com/adventure/adventureland.txt");
Uri three = Uri.parse("http://textfiles.com/adventure/adventur.txt");

notificationOne = IOUtils.toString(one);
notificationTwo = IOUtils.toString(two);
notificationThree = IOUtils.toString(three);
notificationsText.setText(notificationOne);

}


It so far does not convert the Uri to string but instead states it cannot resolve the method .toString(android.net.Uri)

I've imported IOUtils
import org.apache.commons.io.IOUtils;

and enabled internet access in my manifest file
<uses-permission android:name="android.permission.INTERNET" />
.

So, how can I resolve it, or, is there an easier way to convert a Url to a string?

You might want to check the content of the Urls before answering.

This toString method is different than the one used to convert normally in android without IOUtils.

Answer

IOUtils.toString() takes a java.net.URI as its argument. You are passing it an android.net.Uri.

When calling IOUtils.toString(), you should also make sure to pass the content encoding as the second argument.