解析数据org.json.JSONException: Value Not of type of java.lang.S



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

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。这有什么不对吗?

我改变了我的解析器如下(从http://androidapphive.blogspot.com/),它工作完美

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();
    }
}

相关内容

  • 没有找到相关文章

最新更新