图像未从 Glide 库加载
Image not loading from Glide library
图像未从 API 调用中加载。
使用了 Volley 库和 Json 对象请求。
使用 Glide 库加载图像。
API : https://meme-api.herokuapp.com/gimme
JSON 对象:
{
"postLink": "https://redd.it/rotkp8",
"subreddit": "memes",
"title": "It's only 7AM mom.. let me sleep!",
"url": "https://i.redd.it/5bd4d2tmuu781.jpg",
"nsfw": false,
"spoiler": false,
"author": "DragonChasm",
"ups": 298,
"preview": [
"https://preview.redd.it/5bd4d2tmuu781.jpg?width=108&crop=smart&auto=webp&s=d8ef60aaca5d8fe4b3150bcb837103391100845d",
"https://preview.redd.it/5bd4d2tmuu781.jpg?width=216&crop=smart&auto=webp&s=157e9b22042e70e6be24f82d92eca80069ccfea6",
"https://preview.redd.it/5bd4d2tmuu781.jpg?width=320&crop=smart&auto=webp&s=3e64a5592823edf82aa4b2c2081babc3f39597f0"
]
}
// Main代码Activity.
package com.nandini.android.memeshareapp
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.bumptech.glide.Glide
class MainActivity : AppCompatActivity() {
lateinit var memeImageView: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadMeme()
}
private fun loadMeme() {
val queue = Volley.newRequestQueue(this as Context)
val url = "https://meme-api.herokuapp.com/gimme"
memeImageView= findViewById(R.id.memeImageView)
Glide.with(this).load(url).into(memeImageView)
val jsonObjectRequest = JsonObjectRequest(Request.Method.GET, url, null, Response.Listener { response ->
val url = response.getString("url")
}, Response.ErrorListener {
Toast.makeText(this, "Something went wrong.", Toast.LENGTH_LONG).show()
})
queue.add(jsonObjectRequest)
}
}
// XML code.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/memeImageView"
android:src="@drawable/download"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/meme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/shareButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:text="@string/share"
android:padding="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/guideline2"
android:layout_marginStart="5dp" />
<Button
android:id="@+id/nextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:text="@string/next"
android:padding="32dp"
app:layout_constraintLeft_toRightOf="@+id/guideline2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
//清单文件。(添加了互联网权限。)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nandini.android.memeshareapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MemeShareApp">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
// 添加构建中的依赖项 gradle (Module app)
implementation("com.android.volley:volley:1.2.1")
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
我觉得loadMeme
功能不对,应该是这样的
private fun loadMeme() {
val queue = Volley.newRequestQueue(this as Context)
val url = "https://meme-api.herokuapp.com/gimme"
memeImageView= findViewById(R.id.memeImageView)
val jsonObjectRequest = JsonObjectRequest(Request.Method.GET, url, null, Response.Listener { response ->
val url = response.getString("url")
Glide.with(this).load(url).into(memeImageView)
}, Response.ErrorListener {
Toast.makeText(this, "Something went wrong.", Toast.LENGTH_LONG).show()
})
queue.add(jsonObjectRequest)
}
图像未从 API 调用中加载。 使用了 Volley 库和 Json 对象请求。 使用 Glide 库加载图像。 API : https://meme-api.herokuapp.com/gimme
JSON 对象:
{
"postLink": "https://redd.it/rotkp8",
"subreddit": "memes",
"title": "It's only 7AM mom.. let me sleep!",
"url": "https://i.redd.it/5bd4d2tmuu781.jpg",
"nsfw": false,
"spoiler": false,
"author": "DragonChasm",
"ups": 298,
"preview": [
"https://preview.redd.it/5bd4d2tmuu781.jpg?width=108&crop=smart&auto=webp&s=d8ef60aaca5d8fe4b3150bcb837103391100845d",
"https://preview.redd.it/5bd4d2tmuu781.jpg?width=216&crop=smart&auto=webp&s=157e9b22042e70e6be24f82d92eca80069ccfea6",
"https://preview.redd.it/5bd4d2tmuu781.jpg?width=320&crop=smart&auto=webp&s=3e64a5592823edf82aa4b2c2081babc3f39597f0"
]
}
// Main代码Activity.
package com.nandini.android.memeshareapp
import android.content.Context
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import android.widget.ImageView
import android.widget.Toast
import com.android.volley.Request
import com.android.volley.Response
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.bumptech.glide.Glide
class MainActivity : AppCompatActivity() {
lateinit var memeImageView: ImageView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadMeme()
}
private fun loadMeme() {
val queue = Volley.newRequestQueue(this as Context)
val url = "https://meme-api.herokuapp.com/gimme"
memeImageView= findViewById(R.id.memeImageView)
Glide.with(this).load(url).into(memeImageView)
val jsonObjectRequest = JsonObjectRequest(Request.Method.GET, url, null, Response.Listener { response ->
val url = response.getString("url")
}, Response.ErrorListener {
Toast.makeText(this, "Something went wrong.", Toast.LENGTH_LONG).show()
})
queue.add(jsonObjectRequest)
}
}
// XML code.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ImageView
android:id="@+id/memeImageView"
android:src="@drawable/download"
android:layout_width="0dp"
android:layout_height="0dp"
android:contentDescription="@string/meme"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/shareButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:text="@string/share"
android:padding="32dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toLeftOf="@+id/guideline2"
android:layout_marginStart="5dp" />
<Button
android:id="@+id/nextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginLeft="5dp"
android:text="@string/next"
android:padding="32dp"
app:layout_constraintLeft_toRightOf="@+id/guideline2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent" />
<androidx.constraintlayout.widget.Guideline
android:id="@+id/guideline2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.5" />
</androidx.constraintlayout.widget.ConstraintLayout>
//清单文件。(添加了互联网权限。)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.nandini.android.memeshareapp">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.MemeShareApp">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
// 添加构建中的依赖项 gradle (Module app)
implementation("com.android.volley:volley:1.2.1")
implementation 'com.github.bumptech.glide:glide:4.12.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.12.0'
我觉得loadMeme
功能不对,应该是这样的
private fun loadMeme() {
val queue = Volley.newRequestQueue(this as Context)
val url = "https://meme-api.herokuapp.com/gimme"
memeImageView= findViewById(R.id.memeImageView)
val jsonObjectRequest = JsonObjectRequest(Request.Method.GET, url, null, Response.Listener { response ->
val url = response.getString("url")
Glide.with(this).load(url).into(memeImageView)
}, Response.ErrorListener {
Toast.makeText(this, "Something went wrong.", Toast.LENGTH_LONG).show()
})
queue.add(jsonObjectRequest)
}