Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
解析数据org.json.JSONException时出错:从youtube api v3解析json时,无法将非java.lang.String类型的值转换为JSONObject_Json_Youtube_Youtube Api - Fatal编程技术网

解析数据org.json.JSONException时出错:从youtube api v3解析json时,无法将非java.lang.String类型的值转换为JSONObject

解析数据org.json.JSONException时出错:从youtube api v3解析json时,无法将非java.lang.String类型的值转换为JSONObject,json,youtube,youtube-api,Json,Youtube,Youtube Api,我试图从youtube api v3解析json,如下所示 try { JSONParser jParser = new JSONParser(); JSONObject json = jParser .getJSONFromUrl("https://www.googleapis.com/youtube/v3/channels?key=AIzaSyBz9D7Ohyk1hGPJdpsLAPka6rZjPQRjm

我试图从youtube api v3解析json,如下所示

try {
            JSONParser jParser = new JSONParser();
            JSONObject json = jParser
                    .getJSONFromUrl("https://www.googleapis.com/youtube/v3/channels?key=AIzaSyBz9D7Ohyk1hGPJdpsLAPka6rZjPQRjm70&id=UCMyqIESszP2IvTO5iZRu8ZA&part=snippet&fields=items(etag,snippet/title,snippet/description,snippet/thumbnails/high/url)");
            Log.d("okies", "okies the json is "+json);
        }catch (Exception e) {          
            e.printStackTrace();            
        }
下面是我的JSONParser类

public class JSONParser {

    static InputStream is;
    static JSONObject jObj;
    static String json;

    // constructor
    public JSONParser() {
        is = null;
        jObj = null;
        json = "";
    }

    public JSONObject getJSONFromUrl(String url) {

        // Making HTTP request
        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            is = httpEntity.getContent();           

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            json = sb.toString();
            System.out.println("my json is "+json);
        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }

        // try parse the string to a JSON object
        try {
            jObj = new JSONObject(json);
        } catch (JSONException e) {
            jObj = null;

            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        // return JSON String
        //return jObj;
        return jObj;

    }
}
当我在浏览器上使用我的google api键尝试url时,它会显示json,但当我尝试运行程序时,它会给出以下错误

Error parsing data org.json.JSONException: Value Not of type java.lang.String cannot be converted to JSONObject

另外,当我尝试其他json url时,我会得到json。这有什么问题

我更改了解析器,如下所示,它工作得非常好

public class JSONParser {

    public String url=null;

//You can change the playlistId
//You can change the maxResults from 30 to 50 only videos can be fetched at a time
//For more detail go to this link https://developers.google.com/youtube/v3/docs/playlistItems/list#try-it 

    private static StringBuilder sb;
    private JSONObject jObj;    

    public JSONParser2() {
        // TODO Auto-generated constructor stub
    }

    public JSONObject getJsonFromYoutube(String url){        
        DefaultHttpClient httpclient = new DefaultHttpClient();
        Log.e("url",url);        
        HttpGet getRequest = new HttpGet(url);
        getRequest.setHeader("Accept", "application/json");
        // Use GZIP encoding
        getRequest.setHeader("Accept-Encoding", "gzip"); //
        try {
            HttpResponse response = (HttpResponse) httpclient
            .execute(getRequest);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                Header contentEncoding = response.getFirstHeader("Content-Encoding");
                if (contentEncoding != null
                        && contentEncoding.getValue().equalsIgnoreCase("gzip")) {
                    instream = new GZIPInputStream(instream);
                }
                // convert content stream to a String
                String result = readStream(instream);
                Log.i("JSON", result);
                instream.close();
                jObj = new JSONObject(result);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return jObj;
    }
    private static String readStream(InputStream is) {
        BufferedReader reader;
        try {
            reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);

            sb = new StringBuilder();

            String line = null;
            try {
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    is.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        } catch (UnsupportedEncodingException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        }
        return sb.toString();
    }
}