e/volley:[6074]basicnetwork.performrequest:的意外响应代码403

ubbxdtey  于 2021-07-08  发布在  Java
关注(0)|答案(0)|浏览(297)

活动\u main.xml

<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">
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/rView"
        android:layout_width="409dp"
        android:layout_height="729dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
            </androidx.constraintlayout.widget.ConstraintLayout>

2.u frame.xml活动

<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="500dp">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="411dp"
            android:layout_height="360dp"
            android:scaleType="centerCrop"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            tools:srcCompat="@tools:sample/avatars"
            android:contentDescription="@string/todo" />

        <TextView
            android:id="@+id/textView1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="@string/textview1"
            android:textSize="18sp"
            android:textStyle="bold"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.0"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/imageView"
            app:layout_constraintVertical_bias="0.173" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:text="@string/textview2"
            android:maxLines="1"
            android:ellipsize="end"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="@+id/textView1"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView1" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>

hdnews.java文件
包com.example.android.hdnews;

public class HDNews {

    private String title;
    private String author;
    private String image;

    public HDNews(String title, String author, String image) {
        this.title = title;
        this.author = author;
        this.image = image;
    }

    public String getTitle() {
        return title;
    }

    public String getAuthor() {
        return author;
    }

    public String getImage() {
        return image;
    }
}

主活动.java
包com.example.android.hdnews;
导入androidx.appcompat.app.appcompatactivity;导入androidx.recyclerview.widget.linearlayoutmanager;导入androidx.recyclerview.widget.recyclerview;
导入android.os.bundle;导入android.widget.linearlayout;
导入com.android.volley.networkresponse;导入com.android.volley.request;导入com.android.volley.requestqueue;导入com.android.volley.response;导入com.android.volley.volleyerror;导入com.android.volley.toolbox.jsonobjectrequest;导入com.android.volley.toolbox.volley;
导入org.json.jsonarray;导入org.json.jsonexception;导入org.json.jsonobject;导入java.util.arraylist;
公共类mainactivity扩展了appcompatactivity{
私人recyclerview recyclerview;
私有数组列表;
专用hdnewsadapter hdnewsadapter;
私有请求队列;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    recyclerView=findViewById(R.id.rView);
    recyclerView.setHasFixedSize(true);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    al=new ArrayList<HDNews>();
    requestQueue= Volley.newRequestQueue(this);
    extractingDataFromInternet();
}
//http://newsapi.org/v2/top-headlines?country=in&apiKey=94bf8bcc374b494485309e325e3656f9
public void extractingDataFromInternet(){
   String url="http://newsapi.org/v2/top-headlines?country=in&category=business&apiKey=94bf8bcc374b494485309e325e3656f9";
    JsonObjectRequest JsonObjReq=new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {

        @Override
        public void onResponse(JSONObject response) {
            try {
                JSONArray jsonArray = response.getJSONArray("articles");
                for (int i = 0; i < jsonArray.length(); i++) {
                    JSONObject ca = jsonArray.getJSONObject(i);
                    String authorNames = ca.optString("author");
                    String titleNames = ca.optString("title");
                    String imageUrls = ca.getString("urlToImage");
                    al.add(new HDNews(titleNames, authorNames, imageUrls));
                }
                hdNewsAdapter = new HDNewsAdapter(MainActivity.this, al);
                recyclerView.setAdapter(hdNewsAdapter);
            } catch ( JSONException e ) {
                e.printStackTrace();
            }
        }
    }, new Response.ErrorListener() {
       @Override
       public void onErrorResponse(VolleyError error) {
       }
   });
      requestQueue.add(JsonObjReq);
}

}
hdnewsadapter接口
包com.example.android.hdnews;
导入android.content.context;导入android.view.layoutinflater;导入android.view.view;导入android.view.viewgroup;导入android.widget.imageview;导入android.widget.textview;
导入androidx.annotation.nonnull;导入androidx.recyclerview.widget.recyclerview;
导入com.bumptech.glide.glide;
导入java.util.arraylist;
公共类hdnewsadapter扩展了recyclerview.adapter<hdnewsadapter.viewholder>{

private Context context;
private ArrayList<HDNews> al;
public HDNewsAdapter(Context context, ArrayList<HDNews> al) {
    this.context = context;
    this.al = al;
}
@NonNull
@Override
public HDNewsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
    View views= LayoutInflater.from(parent.getContext()).inflate(R.layout.activity_frame,parent,false);
    return new ViewHolder(views);
}

@Override
public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            HDNews currentposition=al.get(position);
            String imgurl=currentposition.getImage();
            String athName=currentposition.getAuthor();
            String ttleName=currentposition.getTitle();
    holder.titleName01.setText(ttleName);
    holder.authorName01.setText(athName);
    Glide.with(context).load(imgurl).into(holder.imageurl01);
}

@Override
public int getItemCount() {
    return al.size();
}

public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    public TextView authorName01;
    public TextView titleName01;
    public ImageView imageurl01;
    public ViewHolder(@NonNull View itemView) {
        super(itemView);
        authorName01=itemView.findViewById(R.id.textView1);
        titleName01=itemView.findViewById(R.id.textView2);
        imageurl01=itemView.findViewById(R.id.imageView);
    }

    @Override
    public void onClick(View view) {

    }
}

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题