Java 我怎样才能提取“引用”;标题「;从这个API?

Java 我怎样才能提取“引用”;标题「;从这个API?,java,android,json,Java,Android,Json,这是我在stackoverflow中看到的一段代码,它使用视频id从youtube获取单个视频的数据。但是,我似乎无法从URL获取“标题”。它可以在我的浏览器中工作,但在我正在开发的android应用程序中,它不能工作。代码怎么了 private class RequestTask extends AsyncTask<String, String, String> { // make a request to the specified url @Override

这是我在stackoverflow中看到的一段代码,它使用视频id从youtube获取单个视频的数据。但是,我似乎无法从URL获取“标题”。它可以在我的浏览器中工作,但在我正在开发的android应用程序中,它不能工作。代码怎么了

private class RequestTask extends AsyncTask<String, String, String> {
    // make a request to the specified url
    @Override
    protected String doInBackground(String... uri) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpResponse response;
        String responseString = null;
        try {
            // make a HTTP request
            response = httpclient.execute(new HttpGet(uri[0]));
            StatusLine statusLine = response.getStatusLine();
            if (statusLine.getStatusCode() == HttpStatus.SC_OK) {
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                response.getEntity().writeTo(out);
                out.close();
                responseString = out.toString();
            } else {
                // close connection
                response.getEntity().getContent().close();
                throw new IOException(statusLine.getReasonPhrase());
            }
        } catch (Exception e) {
            Log.d("Test", "Couldn't make a successful request!");
        }
        return responseString;
    }

    @Override
    protected void onPostExecute(String response) {
        super.onPostExecute(response);

        try {
            // convert the String response to a JSON object
            JSONObject jsonResponse = new JSONObject(response);
            Toast.makeText(getActivity(), jsonResponse.getString("title"), Toast.LENGTH_SHORT).show();

            // fetch the array of movies in the response
            JSONArray jArray = jsonResponse.getJSONArray("movies");


        } catch (JSONException e) {
            Log.d("Test", "Couldn't successfully parse the JSON response!");
        }
    }
}
私有类RequestTask扩展了AsyncTask{
//向指定的url发出请求
@凌驾
受保护的字符串doInBackground(字符串…uri){
HttpClient HttpClient=新的DefaultHttpClient();
HttpResponse响应;
字符串responseString=null;
试一试{
//发出HTTP请求
response=httpclient.execute(新的HttpGet(uri[0]);
StatusLine StatusLine=response.getStatusLine();
if(statusLine.getStatusCode()==HttpStatus.SC\u OK){
ByteArrayOutputStream out=新建ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString=out.toString();
}否则{
//密切联系
response.getEntity().getContent().close();
抛出新IOException(statusLine.getReasonPhrase());
}
}捕获(例外e){
Log.d(“测试”,“无法发出成功的请求!”);
}
回报率;
}
@凌驾
受保护的void onPostExecute(字符串响应){
super.onPostExecute(响应);
试一试{
//将字符串响应转换为JSON对象
JSONObject jsonResponse=新的JSONObject(响应);
Toast.makeText(getActivity(),jsonResponse.getString(“title”),Toast.LENGTH_SHORT.show();
//获取响应中的电影数组
JSONArray jArray=jsonResponse.getJSONArray(“电影”);
}捕获(JSONException e){
Log.d(“Test”,“无法成功解析JSON响应!”);
}
}
}
顺便说一下,这是API

https://www.googleapis.com/youtube/v3/videos?id=P3mAtvs5Elc&key=<INSERT_API_SERVER_KEY_HERE>&fields=items(id,snippet(description,channelId,title,categoryId),statistics)&part=snippet,statistics
https://www.googleapis.com/youtube/v3/videos?id=P3mAtvs5Elc&key=&fields=items(id,代码段(描述,通道id,标题,类别id),统计信息)&部分=代码段,统计信息

以下是YOuTube API响应的样子:

{
 "kind": "youtube#videoListResponse",
 "etag": "\"UCBpFjp2h75_b92t44sqraUcyu0/sDAlsG9NGKfr6v5AlPZKSEZdtqA\"",
 "videos": [
  {
   "id": "7lCDEYXw3mM",
   "kind": "youtube#video",
   "etag": "\"UCBpFjp2h75_b92t44sqraUcyu0/iYynQR8AtacsFUwWmrVaw4Smb_Q\"",
   "snippet": {
    "publishedAt": "2012-06-20T22:45:24.000Z",
    "channelId": "UC_x5XG1OV2P6uZZ5FSM9Ttw",
    "title": "Google I/O 101: Q&A On Using Google APIs",
    "description": "Antonio Fuentes speaks to us and takes questions on working with Google APIs and OAuth 2.0.",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/7lCDEYXw3mM/hqdefault.jpg"
     }
    },
    "categoryId": "28"
   },
   "contentDetails": {
    "duration": "PT15M51S",
    "aspectRatio": "RATIO_16_9"
   },
   "statistics": {
    "viewCount": "3057",
    "likeCount": "25",
    "dislikeCount": "0",
    "favoriteCount": "17",
    "commentCount": "12"
   },
   "status": {
    "uploadStatus": "STATUS_PROCESSED",
    "privacyStatus": "PRIVACY_PUBLIC"
   }
  }
 ]
}
要获取视频标题,请使用以下代码:

JSONObject responseObject = new JSONObject(response);
JSONArray videosArray = jsonObject.getJSONArray("videos");
JSONObject videoObject = videosArray.getJSONObject(0); //Pointing to first video
JSONObject snippetObject = videoObject.getJSONObject("snippet");
String title = snippetObject.getString("title");

不要使用
jsonResponse.getString(“title”)
尝试使用
jsonResponse.getJSONObject(“title”).toString()
。记录您的响应是否获得了标题。发布您得到的响应或异常。