Get Json Object In Android
I have article view where each post of mine will show details (think of it as blog post) but i cannot parse the data so i get my app closed instead of details of my articles. code
Solution 1:
Actually the name of your Array is categories not article
so in this line of your code val jsonArray = jsonObject.getJSONArray("article")
, instead of article you need to add categories
something like below
val jsonArray = jsonObject.getJSONArray("categories")
EDIT 1 :-
As discussed your article_details.xml
will be like below
Here you can change size
and margins
, as you want
<?xml version="1.0" encoding="utf-8"?><androidx.constraintlayout.widget.ConstraintLayoutxmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"android:layout_width="match_parent"android:layout_height="match_parent"><ImageViewandroid:id="@+id/article_image"android:layout_width="match_parent"android:layout_height="150dp"android:layout_marginStart="15dp"android:layout_marginEnd="15dp"android:src="@mipmap/ic_launcher"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" /><TextViewandroid:id="@+id/username"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="30dp"android:layout_marginTop="15dp"android:maxLines="1"android:text="userName"android:textSize="14sp"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/article_image" /><TextViewandroid:id="@+id/article_name"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginStart="30dp"android:layout_marginTop="5dp"android:maxLines="1"android:text="Article name"android:textSize="18sp"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/username" /><TextViewandroid:id="@+id/article_body"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginStart="15dp"android:layout_marginTop="10dp"android:layout_marginEnd="15dp"android:text="Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. "android:textSize="20sp"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toBottomOf="@+id/article_name" /></androidx.constraintlayout.widget.ConstraintLayout>
Now just do the findfindViewById
in your ArticlesDetail
class so you can set values on them when you receive data from your API
EDIT 2:-
Now Your ArticlesDetail
will be like
classArticlesDetail : AppCompatActivity() {
privatelateinitvar appBarConfiguration: AppBarConfiguration
var username: TextView? = nullvar articleName: TextView? = nullvar articleBody: TextView? = nullvar articleImage: ImageView? = nulloverridefunonCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
username = findViewById(R.id.username)
articleName = findViewById(R.id.article_name)
articleBody = findViewById(R.id.article_body)
articleImage = findViewById(R.id.article_image)
callAPIDemo()
}
// api codeprivatefuncallAPIDemo() {
val mySlugValue: String = intent.getStringExtra("my_slug")
Log.d("myslug in:", mySlugValue)
// Instantiate the RequestQueue.val queue = Volley.newRequestQueue(this)
val url = "https://example.com/api/articles/$mySlugValue"// Request a string response from the provided URL.val stringRequest = StringRequest(
Request.Method.GET, url,
Response.Listener<String> { response ->
parseAndSetValues(response)
},
Response.ErrorListener { error ->
//displaying the error in toast if occurrs
Toast.makeText(applicationContext, error.message, Toast.LENGTH_SHORT)
.show()
})
// Add the request to the RequestQueue.
queue.add(stringRequest)
}
privatefunparseAndSetValues(response: String) {
val jsonObject = JSONObject(response)
username!!.text = jsonObject.getString("user")
articleName!!.text = jsonObject.getString("name")
articleBody!!.text = jsonObject.getString("body")
Glide.with(this).load(jsonObject.getString("image")).into(articleImage!!)
}
}
Solution 2:
There is no JSONArray named 'article' in your response JSON. Thats why its causing the error remove this part
valjsonArray= jsonObject.getJSONArray("article")
for (i in 0 until jsonArray.length()) {
valjsonObject1= jsonArray.getJSONObject(i)
varlistingObject= Article(
jsonObject1.getInt("id"),
jsonObject1.getString("name"),
jsonObject1.getString("slug"),
jsonObject1.getString("image"),
jsonObject1.getString("body"),
jsonObject1.getString("icon"),
jsonObject1.getString("quote"),
jsonObject1.getString("video"),
jsonObject1.getString("user"),
jsonObject1.getString("created_at"),
jsonObject1.getString("updated_at")
)
list.add(listingObject)
}
add this
Intid= jsonObject.getInt("id"),
Stringname= jsonObject.getString("name"),
Stringslug= jsonObject.getString("slug"),
Stringimage= jsonObject.getString("image"),
Stringbody= jsonObject.getString("body"),
Stringicon= jsonObject.getString("icon"),
Stringquote= jsonObject.getString("quote"),
Stringvideo= jsonObject.getString("video"),
Stringuser= jsonObject.getString("user"),
Stringcreated_at= jsonObject.getString("created_at"),
Stringupdated_at= jsonObject.getString("updated_at")
``
Post a Comment for "Get Json Object In Android"