Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.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
Android:JSONException从Rails API加载数据时_Android_Ruby On Rails_Json_Parsing - Fatal编程技术网

Android:JSONException从Rails API加载数据时

Android:JSONException从Rails API加载数据时,android,ruby-on-rails,json,parsing,Android,Ruby On Rails,Json,Parsing,我在Android应用程序中解析Rails API的JSON响应时遇到问题。 我的Android应用程序中有相当标准的json解析器。当我使用教程中的一些JSON URL(如以下内容)为它提供信息时,它似乎工作得很好。 但一旦我把我的链接(我使用隧道连接到我的本地主机)放在那里,它就无法解析任何数据,并给出 JSONException: Value of type java.lang.String cannot be converted to JSONObject 所以我认为问题在于我的服务器

我在Android应用程序中解析Rails API的JSON响应时遇到问题。 我的Android应用程序中有相当标准的json解析器。当我使用教程中的一些JSON URL(如以下内容)为它提供信息时,它似乎工作得很好。 但一旦我把我的链接(我使用隧道连接到我的本地主机)放在那里,它就无法解析任何数据,并给出

JSONException: Value of type java.lang.String cannot be converted to JSONObject
所以我认为问题在于我的服务器响应

{
"items": [
    {
        "title": "Sometitle",
        "address": "Someaddress"
    },
    {
        "title": "Sometitle2",
        "address": "Someaddress2"
    }
]
}
json验证器表明它是有效的。我研究了类似的问题,没有找到解决方案,我不知道原因是什么。有人能提出解决方案吗

UPD.我的实际Android代码:

public class JsonParser {

final String TAG = "JsonParser.java";

static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

public JSONObject getJSONFromUrl(String url) {

    // make HTTP request
    try {

        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).append("\n");
        }
        is.close();
        json = sb.toString();

    } catch (Exception e) {
        Log.e(TAG, "Error converting result " + e.toString());
    }

    // try parse the string to a JSON object
    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e(TAG, "Error parsing data " + e.toString());
    }

    // return JSON String
    return jObj;
}
}

然后在我的活动中:

          JsonParser jParser = new JsonParser();

          // get json string from url
          JSONObject json = jParser.getJSONFromUrl(ITEMS_URL);

          // get the array of items
          dataJsonArr = new JSONArray(json);

这可能是服务器端的问题,在您点击服务器获取响应的方法中,您必须首先以字符串变量获取响应,尝试从那里获取响应,可能从那里获得正确的响应。

实际问题是使用HttpPost进行请求,该请求应为HttpGet


由于服务器端的请求映射,API的响应可能是错误响应。

您需要在应用程序中发布尝试解析JSON的代码。“浏览器中的响应看起来像…”:这是Android设备上的浏览器还是桌面上的浏览器?@Ascorbin已用解析器更新code@Vla:在这种情况下,请在桌面浏览器中再次查看它,然后查看源代码-其中可能有其他内容(例如HTML标记等)@Squonk实际上在浏览器中似乎是html样式的,但为什么在线json验证器显示url中的json是有效的?另一个问题是,我在Cyrylic字母中有一些字符串,这可能是原因吗?我不确定。我尝试过这个,但情况仍然是一样的-我的API url出现错误,教程中的一个url没有错误。这应该是Url问题。我相信如果你使用curl,API会有什么反应?你在Android代码中使用的方法正确吗?GET还是POST?只是想知道。你可能在浏览器中使用Postman来发布文章,但这并不明显。这是一个简单的GET请求。curl呢?这很奇怪:
{“items”:[{“title”:“\u0421\u0432\u0456\u0442\u0430\u043d\u043e\u043a”,“地址:”\u0443\u0440\u0413\u0440\u044f\u0434\u0430“},{……
Cyrylic的东西似乎被编码了。这会导致问题吗?那么我想你想要的是一个HttpGet,而不是DefaultHttpClient-httpClient-httpClient=new-DefaultHttpClient();HttpPost-HttpPost=new-HttpPost(url);API接收到错误的HttpMethod,并且您可能在设备代码中接收到错误消息。好的,这不是西里尔语问题-我更改了API代码以向我发送dumy英语数据-问题仍然存在。