我想从某些 api 获取数据,我不知道我的 logcat 中哪里出了问题,它显示错误,意外的响应代码 403 这是我的主要活动
public class MainActivity extends AppCompatActivity {
RequestQueue queue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
queue=MySingleton.getInstance(this).getmRequestQueue();
getList();
}
ArrayList getList(){
JsonObjectRequest jsonObjectRequest =new JsonObjectRequest(Request.Method.GET,
"https://newsapi.org/v2/top-headlines?country=in&apiKey=e613af58839749aab8f66bba967ab5a8", null
, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
String val=response.getString("status");
Log.d("json","status : "+val);
//i want to see this val in my logcat but its giving an error
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(jsonObjectRequest);
return questionsobjectList;
}
Run Code Online (Sandbox Code Playgroud)
这是我的 logcat 请帮助我解决这个错误我是初学者
2020-10-28 22:52:41.477 21412-21438/com.e.practice E/gralloc: Arm Module v1.0
2020-10-28 22:52:41.478 21412-21438/com.e.practice E/ion: ioctl c0044901 failed with code -1: Invalid argument
2020-10-28 22:52:41.479 21412-21438/com.e.practice W/gralloc: WARNING: internal format modifier bits not mutually exclusive. AFBC basic bit is always set, so extended AFBC support bits must always be checked.
2020-10-28 22:52:41.523 21412-21412/com.e.practice I/Choreographer: Skipped 1 frames! The application may be doing too much work on its main thread.
2020-10-28 22:52:41.682 21412-21448/com.e.practice I/System.out: [socket] e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaUtils
2020-10-28 22:52:43.174 21412-21448/com.e.practice E/Volley: [11812] BasicNetwork.performRequest: Unexpected response code 403 for https://newsapi.org/v2/top-headlines?country=in&apiKey=e613af58839749aab8f66bba967ab5a8
2020-10-28 22:52:43.190 21412-21448/com.e.practice I/System.out: [socket] e:java.lang.ClassNotFoundException: com.mediatek.cta.CtaUtils
2020-10-28 22:52:44.553 21412-21448/com.e.practice D/Volley: [11812] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] https://newsapi.org/v2/top-headlines?country=in&apiKey=e613af58839749aab8f66bba967ab5a8 0xc16ea6dd NORMAL 1> [lifetime=3147], [size=12664], [rc=403], [retryCount=1]
2020-10-28 22:52:44.554 21412-21448/com.e.practice E/Volley: [11812] BasicNetwork.performRequest: Unexpected response code 403 for https://newsapi.org/v2/top-headlines?country=in&apiKey=e613af58839749aab8f66bba967ab5a8
2020-10-28 22:58:27.524 21412-21412/com.e.practice D/ColorViewRootUtil: nav gesture mode swipeFromBottom ignore true downY 1021 mScreenHeight 2400 mScreenWidth 1080 mStatusBarHeight 54 globalScale 1.125 nav mode 3 event MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=22.0, y[0]=1021.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=55841499, downTime=55841499, deviceId=3, source=0x1002, displayId=0 } rotation 0
2020-10-28 22:58:27.556 21412-21412/com.e.practice D/ColorViewRootUtil: do not ignore inject event MotionEvent:MotionEvent { action=ACTION_DOWN, actionButton=0, id[0]=0, x[0]=22.0, y[0]=1021.0, toolType[0]=TOOL_TYPE_FINGER, buttonState=0, classification=NONE, metaState=0, flags=0x0, edgeFlags=0x0, pointerCount=1, historySize=0, eventTime=55841499, downTime=55841498, deviceId=3, source=0x1002, displayId=0 }
Run Code Online (Sandbox Code Playgroud)
小智 5
向请求添加自定义 getHeader 会有所帮助。为此,请在构造请求之前添加object:以及下面提到的 getHeader 函数。
private fun fetchData() {
//volly library
val url = "http://newsapi.org/v2/top-headlines?country=in&apiKey=a13cd2a274a84257ad4b8ce468e0180a"
//making a request
val jsonObjectRequest = object: JsonObjectRequest(
Request.Method.GET,
url,
null,
Response.Listener {
val newsJsonArray = it.getJSONArray("articles")
val newsArray = ArrayList<News>()
for(i in 0 until newsJsonArray.length()) {
val newsJsonObject = newsJsonArray.getJSONObject(i)
val news = News(
newsJsonObject.getString("title"),
newsJsonObject.getString("author"),
newsJsonObject.getString("url"),
newsJsonObject.getString("urlToImage")
)
newsArray.add(news)
}
mAdapter.updateNews(newsArray)
},
Response.ErrorListener {
}
) {
override fun getHeaders(): MutableMap<String, String> {
val headers = HashMap<String, String>()
headers["User-Agent"] = "Mozilla/5.0"
return headers
}
}
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest)
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4931 次 |
| 最近记录: |