我正试图从rest API得到响应,并且我已经按照API文档和凌空文档的所有步骤进行了操作。但这似乎每次都会出错。
我已经在浏览器中直接尝试了API URL,并且响应很好,这意味着我的代码中肯定有错误。
public class MainActivity extends AppCompatActivity {
public void apiclick(View view) throws JSONException {
final TextView textView = (TextView) findViewById(R.id.TextView);
textView.setMovementMethod(new ScrollingMovementMethod());
VolleyLog.DEBUG = true;
// ...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
EditText query = (EditText)findViewById(R.id.query);
String jeff = query.getText().toString();
final String key = "796c9ca2788d1e243f8ec9e8e67acb33";
String url = ("https://api.themoviedb.org/3/search/movie?page=1&query=avengers&api_key=796c9ca2788d1e243f8ec9e8e67acb33" );
// Request a string response from the provided URL.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
// Display the first 1000 characters of the response string.
textView.setText("Response is: " + response.toString());
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
textView.setText("Error");
}
});
// Add the request to the RequestQueue.
queue.add(jsonObjectRequest);
这是我的日志输出。
2019-07-14 10:55:34.544 16130-16432/com.example.companieshousegraphexplorer02 V/Volley: [40536] CacheDispatcher.run: start new dispatcher
2019-07-14 10:55:34.558 16130-16432/com.example.companieshousegraphexplorer02 D/Volley: [40536] WaitingRequestManager.maybeAddToWaitingRequests: new request, sending to network https://api.themoviedb.org/3/search/movie?page=1&query=avengers&api_key=796c9ca2788d1e243f8ec9e8e67acb33
2019-07-14 10:55:34.569 16130-16433/com.example.companieshousegraphexplorer02 D/NetworkSecurityConfig: No Network Security Config specified, using platform default
2019-07-14 10:55:34.580 16130-16130/com.example.companieshousegraphexplorer02 D/Volley: [1] MarkerLog.finish: (22 ms) [ ] https://api.themoviedb.org/3/search/movie?page=1&query=avengers&api_key=796c9ca2788d1e243f8ec9e8e67acb33 0x89bd75e7 NORMAL 1
2019-07-14 10:55:34.581 16130-16130/com.example.companieshousegraphexplorer02 D/Volley: [1] MarkerLog.finish: (+0 ) [ 1] add-to-queue
2019-07-14 10:55:34.581 16130-16130/com.example.companieshousegraphexplorer02 D/Volley: [1] MarkerLog.finish: (+1 ) [40536] cache-queue-take
2019-07-14 10:55:34.582 16130-16130/com.example.companieshousegraphexplorer02 D/Volley: [1] MarkerLog.finish: (+0 ) [40536] cache-miss
2019-07-14 10:55:34.582 16130-16130/com.example.companieshousegraphexplorer02 D/Volley: [1] MarkerLog.finish: (+2 ) [40537] network-queue-take
2019-07-14 10:55:34.582 16130-16130/com.example.companieshousegraphexplorer02 D/Volley: [1] MarkerLog.finish: (+17 ) [40537] post-error
2019-07-14 10:55:34.583 16130-16130/com.example.companieshousegraphexplorer02 D/Volley: [1] MarkerLog.finish: (+2 ) [ 1] done