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
字符串无法转换为JSONArray Android_Android_Json - Fatal编程技术网

字符串无法转换为JSONArray Android

字符串无法转换为JSONArray Android,android,json,Android,Json,我试图从谷歌url api中提取长url。以下是代码片段,我正在尝试继续: `StringBuilder url=新的StringBuilder(url); append(shortUrl) 包含代码的函数返回一个JSON对象。在logcat中,我看到一个错误org.json.JSONException:Valuehttp://www.google.com/ 无法将java.lang.String类型的at longUrl转换为JSONArray 代码正在接收完整的url(),但我对JSONAr

我试图从谷歌url api中提取长url。以下是代码片段,我正在尝试继续: `StringBuilder url=新的StringBuilder(url); append(shortUrl)

包含代码的函数返回一个JSON对象。在logcat中,我看到一个错误
org.json.JSONException:Valuehttp://www.google.com/ 无法将java.lang.String类型的at longUrl转换为JSONArray


代码正在接收完整的url(),但我对JSONArray做了一些错误。

尝试使用jsonObj.getString(“longUrl”)而不是jsonObj.getJSONArray(“longUrl”),因为“longUrl”是字符串而不是数组,当然,将结果分配到字符串而不是JSONArray中。

附带说明:这是来自Google API的响应:
{“种类”:“urlshortener#url”,“id”:http://goo.gl/fbsS,“longUrl”:http://www.google.com/“,”状态“:“确定”}
    HttpGet get = new HttpGet(url.toString());
    HttpResponse r = client.execute(get);
    int status = r.getStatusLine().getStatusCode();
    if(status == 200){
        HttpEntity e = r.getEntity();
        String data = EntityUtils.toString(e);

        JSONObject jsonObj = new JSONObject(data);
        JSONArray ja = jsonObj.getJSONArray("longUrl");

        JSONObject last = ja.getJSONObject(0);
        return last;