[英]How to parse JSON API in android
我正在开发一个应用程序,其中有一个 Json api,我需要在 recyclerview 中填充它。 它是一个新闻 api(不是来自 newsapi.org)。 我是解析json的初学者。 但是,我尝试过,但除了空屏幕之外什么也没出现。
Json api 是https://earnezy.in/android_shop/newsapi2.php
我附上了代码。
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.Volley;
import com.google.gson.JsonObject;
import com.news.newsapp.R;
import com.news.newsapp.adapters.Adapter;
import com.news.newsapp.model.News;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private final String JSON_URL = "https://earnezy.in/android_shop/newsapi2.php";
private JsonArrayRequest jsonArrayRequest;
private RequestQueue requestQueue;
private List<News> newsList;
private RecyclerView recyclerView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.news);
jsonRequest();
}
private void jsonRequest() {
jsonArrayRequest = new JsonArrayRequest(JSON_URL, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
JSONObject jsonObject = null;
for (int i=0;i<response.length();i++){
try {
jsonObject = response.getJSONObject(i);
News news = new News();
news.setAuthor(jsonObject.getString("author"));
news.setDescription(jsonObject.getString("description"));
news.setTitle(jsonObject.getString("title"));
news.setPublishedAt(jsonObject.getString("publishedAt"));
newsList.add(news);
} catch (JSONException e) {
e.printStackTrace();
}
}
setUpRecyclerView(newsList);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(jsonArrayRequest);
}
private void setUpRecyclerView(List<News> newsList) {
Adapter adapter = new Adapter(MainActivity.this, newsList);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setAdapter(adapter);
}
}
<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:background="#000"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.MainActivity">
<TextView
android:id="@+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="15dp"
android:text="Top Headlines"
android:textColor="#fff"
android:textSize="22dp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/news"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView"
app:layout_constraintVertical_bias="0.034" />
</androidx.constraintlayout.widget.ConstraintLayout>
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.load.resource.drawable.DrawableTransitionOptions;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.RequestOptions;
import com.bumptech.glide.request.target.Target;
import com.news.newsapp.R;
import com.news.newsapp.activities.Utils;
import com.news.newsapp.model.News;
import java.util.List;
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder>{
private Context context;
private List<News> news;
public Adapter(Context context, List<News> news) {
this.context = context;
this.news = news;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view;
LayoutInflater inflater = LayoutInflater.from(context);
view = inflater.inflate(R.layout.news_item, parent, false);
return new MyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull final MyViewHolder holder, int position) {
final MyViewHolder holders = holder;
News model = news.get(position);
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(Utils.getRandomDrawbleColor());
requestOptions.error(Utils.getRandomDrawbleColor());
requestOptions.diskCacheStrategy(DiskCacheStrategy.ALL);
requestOptions.centerCrop();
Glide.with(context)
.load(model.getUrlToImage())
.apply(requestOptions)
.listener(new RequestListener<Drawable>() {
@Override
public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
holder.progressBar.setVisibility(View.GONE);
return false;
}
@Override
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
holder.progressBar.setVisibility(View.GONE);
return false;
}
})
.transition(DrawableTransitionOptions.withCrossFade())
.into(holder.imageView);
holder.author.setText(news.get(position).getAuthor());
holder.description.setText(news.get(position).getDescription());
holder.title.setText(news.get(position).getTitle());
holder.time.setText("\u2022" + Utils.DateToTimeFormat(model.getPublishedAt()));
holder.publishedAd.setText(Utils.DateFormat(model.getPublishedAt()));
}
@Override
public int getItemCount() {
return news.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView author, title, description, time, publishedAd;
ImageView imageView;
ProgressBar progressBar;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
}
}
}
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import org.ocpsoft.prettytime.PrettyTime;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Random;
public class Utils {
public static ColorDrawable[] vibrantLightColorList =
{
new ColorDrawable(Color.parseColor("#ffeead")),
new ColorDrawable(Color.parseColor("#93cfb3")),
new ColorDrawable(Color.parseColor("#fd7a7a")),
new ColorDrawable(Color.parseColor("#faca5f")),
new ColorDrawable(Color.parseColor("#1ba798")),
new ColorDrawable(Color.parseColor("#6aa9ae")),
new ColorDrawable(Color.parseColor("#ffbf27")),
new ColorDrawable(Color.parseColor("#d93947"))
};
public static ColorDrawable getRandomDrawbleColor() {
int idx = new Random().nextInt(vibrantLightColorList.length);
return vibrantLightColorList[idx];
}
public static String DateToTimeFormat(String oldstringDate){
PrettyTime p = new PrettyTime(new Locale(getCountry()));
String isTime = null;
try {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'",
Locale.ENGLISH);
Date date = sdf.parse(oldstringDate);
isTime = p.format(date);
} catch (ParseException e) {
e.printStackTrace();
}
return isTime;
}
public static String DateFormat(String oldstringDate){
String newDate;
SimpleDateFormat dateFormat = new SimpleDateFormat("E, d MMM yyyy", new Locale(getCountry()));
try {
Date date = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'").parse(oldstringDate);
newDate = dateFormat.format(date);
} catch (ParseException e) {
e.printStackTrace();
newDate = oldstringDate;
}
return newDate;
}
public static String getCountry(){
Locale locale = Locale.getDefault();
String country = String.valueOf(locale.getCountry());
return country.toLowerCase();
}
}
public class News {
private String author;
private String title;
private String description;
private String url;
private String publishedAt;
private String urlToImage;
public News() {
}
public News(String author, String title, String description, String url, String publishedAt, String urlToImage) {
this.author = author;
this.title = title;
this.description = description;
this.url = url;
this.publishedAt = publishedAt;
this.urlToImage = urlToImage;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getPublishedAt() {
return publishedAt;
}
public void setPublishedAt(String publishedAt) {
this.publishedAt = publishedAt;
}
public String getUrlToImage() {
return urlToImage;
}
public void setUrlToImage(String urlToImage) {
this.urlToImage = urlToImage;
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<androidx.cardview.widget.CardView
android:id="@+id/cardView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="11dp"
android:layout_marginRight="11dp"
android:layout_marginTop="7dp"
android:layout_marginBottom="7dp"
app:cardBackgroundColor="#000"
app:cardElevation="@dimen/cardview_default_elevation"
app:cardCornerRadius="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/img"
android:layout_width="match_parent"
android:layout_height="200dp"
android:scaleType="centerCrop"
android:transitionName="img"
tools:ignore="UnusedAttribute"/>
<ImageView
android:id="@+id/shadow_image"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_alignBottom="@+id/img"
android:src="@drawable/bottom_shadow"/>
<ProgressBar
android:id="@+id/progress_load_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
style="@android:style/Widget.ProgressBar.Small"/>
<TextView
android:id="@+id/author"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:ellipsize="end"
android:maxLines="1"
android:textColor="#fff"
android:singleLine="true"
android:layout_marginRight="160dp"
android:layout_marginLeft="10dp"
android:text="Author"
android:gravity="bottom"
android:layout_alignLeft="@+id/title"
android:layout_alignStart="@+id/title"
android:layout_alignRight="@+id/layoutDate"
android:layout_alignTop="@+id/layoutDate"
android:layout_alignEnd="@+id/layoutDate"/>
<FrameLayout
android:id="@+id/layoutDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/img"
android:background="#fff"
android:padding="5dp"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:layout_marginTop="-50dp">
<ImageView
android:src="@drawable/ic_date"
android:layout_width="18dp"
android:layout_height="18dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/publishedAt"
android:textColor="#606060"
android:layout_marginLeft="27dp"
android:layout_marginRight="10dp"
android:text="01 January 1999"/>
</FrameLayout>
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/img"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="10dp"
android:text="Title"
android:textColor="#fff"
android:textSize="17sp" />
<TextView
android:id="@+id/desc"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="5dp"
android:text="Desc"
android:textColor="#fff" />
<TextView
android:id="@+id/source"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="26dp"
android:text="Source"
android:textColor="#fff" />
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="46dp"
android:text="Time"
android:textColor="#fff" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</FrameLayout>
来自https://earnezy.in/android_shop/newsapi2.php的回复是一个 JSON 对象而不是数组。 所以请使用下面的代码来正确获取新闻项目。
private void jsonRequest() {
jsonObjectRequest = new JsonObjectRequest(Request.Method.GET,JSON_URL, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
JSONArray articles = response.optJSONArray("articles")
JSONObject jsonObject = null;
for (int i=0;i<articles.length();i++){
try {
jsonObject = articles.getJSONObject(i);
News news = new News();
news.setAuthor(jsonObject.getString("author"));
news.setDescription(jsonObject.getString("description"));
news.setTitle(jsonObject.getString("title"));
news.setPublishedAt(jsonObject.getString("publishedAt"));
newsList.add(news);
} catch (JSONException e) {
e.printStackTrace();
}
}
setUpRecyclerView(newsList);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
requestQueue = Volley.newRequestQueue(MainActivity.this);
requestQueue.add(jsonObjectRequest);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.