package com.example.murarilal.volley;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Base64;
import android.util.Log;
import android.view.View;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class MainActivity extends AppCompatActivity {
RequestQueue queue;
private String url="https://newsapi.org/v1/articles?source=techcrunch&apiKey=59b308aec9f242fe98b527ab9ba93199";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
queue= Volley.newRequestQueue(this);
home();
}
public void home()
{
JsonObjectRequest request =new JsonObjectRequest(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Log.i("website content",response.getString("author"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
});
queue.add(request);
}
}
11-04 17:43:04.272 3687-3772 / com.example.murarilal.volley E / EGL_emulation:tid 3772:eglSurfaceAttrib(1223):错误0x3009(EGL_BAD_MATCH) 11-04 17:43:04.272 3687-3772 / com.example.murarilal.volley W / OpenGLRenderer:无法在表面0x9d77f060上设置EGL_SWAP_BEHAVIOR,错误= EGL_BAD_MATCH 11-04 17:43:04.276 3687-3772 / com.example.murarilal.volley D / EGL_emulation:eglMakeCurrent:0x9d704420:ver 2 0(tinfo 0x9d703460) 11-04 17:43:05.333 3687-3687 / com.example.murarilal.volley W / System.err:org.json.JSONException:没有作者的价值 11-04 17:43:05.333 3687-3687 / com.example.murarilal.volley W / System.err:at org.json.JSONObject.get(JSONObject.java:392) 11-04 17:43:05.333 3687-3687 / com.example.murarilal.volley W / System.err:at org.json.JSONObject.getString(JSONObject.java:553) 11-04 17:43:05.333 3687-3687 / com.example.murarilal.volley W / System.err:at com.example.murarilal.volley.MainActivity $ 1.onResponse(MainActivity.java:47) 11-04 17:43:05.333 3687-3687 / com.example.murarilal.volley W / System.err:at com.example.murarilal.volley.MainActivity $ 1.onResponse(MainActivity.java:41) 11-04 17:43:05.333 3687-3687 / com.example.murarilal.volley W / System.err:at com.android.volley.toolbox.JsonRequest.deliverResponse(JsonRequest.java:65) 11-04 17:43:05.333 3687-3687 / com.example.murarilal.volley W / System.err:at com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 11-04 17:43:05.334 3687-3687 / com.example.murarilal.volley W / System.err:at android.os.Handler.handleCallback(Handler.java:769) 11-04 17:43:05.334 3687-3687 / com.example.murarilal.volley W / System.err:at android.os.Handler.dispatchMessage(Handler.java:98) 11-04 17:43:
**"status": "ok",
"source": "the-next-web",
"sortBy": "latest",
-"articles": [
-{
"author": "Camille Charluet",
"title": "This tech festival is implanting microchips instead of using tickets",
"description": "Human microchipping has always been a pursuit reserved for those crazy guinea pig types excited for the dystopian future everyone else is dreading. As it turns out, 2017 seems to be ...",
"url": "https://thenextweb.com/distract/2017/11/01/tech-festival-implanting-microchips-instead-of-tickets/",
"urlToImage": "https://cdn0.tnwcdn.com/wp-content/blogs.dir/1/files/2017/11/wifi-hand-2-e1509553873488.png",
"publishedAt": "2017-11-01T18:10:26Z"
},
**
答案 0 :(得分:0)
作者字段不在您的json响应的根目录下,因此您无法使用response.getString(“author”)获取它。
相反,您需要遍历“articles”对象数组并从每篇文章中获取作者字段。
答案 1 :(得分:0)
由于作者在数组
之内printf()
这将打印我们的作者。