I am using TheMovieDB API to call the JSON Response of the popular movies and put it into a ScrollView. I have ensured that I have done all the neccessary steps to get the API. However it does not work and does not display anything. If I use another API: "https://jsonplaceholder.typicode.com/posts", it works and the JSON data is displayed into the ScrollView.
Network Utils:
companion object {
private val TAG: String = NetworkUtils::class.java!!.simpleName
private val JSON_RESPONSE_URL = "https://api.themoviedb.org/3/movie/popular?api_key=myapikeyishere"
private val TYPE_SINGLE = 1
private val TYPE_ALL = 0
/**
* Builds the URL used to talk to the weather server using a location. This location is based
* on the query capabilities of the weather provider that we are using.
*
* @param locationQuery The location that will be queried for.
* @return The URL to use to query the weather server.
*/
fun buildURLSingleType(id: Int): URL {
return buildUrl(
TYPE_SINGLE,
id
)
}
fun buildURLAll(): URL {
return buildUrl(
TYPE_ALL,
0
)
}
private fun buildUrl(type: Int, id: Int): URL {
var uri = Uri.parse(JSON_RESPONSE_URL).buildUpon()
if (type == TYPE_SINGLE) {
uri.appendPath("1").build()
}
val builtUri = uri.build()
var url: URL? = null
try {
url = URL(builtUri.toString())
} catch (e: MalformedURLException) {
e.printStackTrace()
}
Log.v(TAG, "Built URI " + url!!)
return url
}
/**
* This method returns the entire result from the HTTP response.
*
* @param url The URL to fetch the HTTP response from.
* @return The contents of the HTTP response.
* @throws IOException Related to network and stream reading
*/
@Throws(IOException::class)
fun getResponseFromHttpUrl(url: URL): String? {
val urlConnection = url.openConnection() as HttpURLConnection
try {
val `in` = urlConnection.getInputStream()
val scanner = Scanner(`in`)
scanner.useDelimiter("\\A")
val hasInput = scanner.hasNext()
return if (hasInput) {
scanner.next()
} else {
null
}
} catch (ex: Exception) {
Log.d(TAG, ex.toString())
} finally {
urlConnection.disconnect()
}
return null
}
}
The Activity:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_network)
}
override fun onResume() {
super.onResume()
val scope = CoroutineScope(Job() + Dispatchers.IO)
var nwMutipleItemsJob = scope.async(Dispatchers.IO)
{
var nwURL = NetworkUtils.buildURLAll()
val response = NetworkUtils.getResponseFromHttpUrl(nwURL)
response
}
scope.async(Dispatchers.Default)
{
var response = nwMutipleItemsJob.await()
var jsonResponse = JSONArray(response)
var msg = "$response\n\n"
for(i in 0 until jsonResponse.length())
{
var jsonItem = jsonResponse.getJSONObject(i)
// var userid = jsonItem.getInt("userId")
// var id = jsonItem.getInt("adult")
// var title = jsonItem.getString("title")
// var body = jsonItem.getString("body")
// msg += "item $i\n\nid = \n = $id\n"
}
withContext(Dispatchers.Main)
{
tvJSONMultipleItemDisplay.text = msg
}
}
}
}
layout activity:
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvJSONMultipleItemDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
</ScrollView>