【问题标题】:Android JSONException: Value <script of type java.lang.String cannot be converted to JSONObjectAndroid JSONException:值 <script of type java.lang.String 无法转换为 JSONObject
【发布时间】:2016-01-05 08:04:24
【问题描述】:

我看到发布了有关此问题的相关问题,但没有一个解决方案对我有用。在我的场景中,尝试从后端发送的 JSON 响应中检索值时出现此错误。

这是来自后端的原始 JSON 响应:

HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Length: 516
Content-Type: application/json; charset=utf-8
Expires: 0
Server: Microsoft-IIS/8.0
X-Powered-By: ASP.NET
Set-Cookie: ARRAffinity=0b;Path=/;Domain=MY-SERVICE-NAME-HERE.azure-mobile.net
Date: Tue, 05 Jan 2016 07:55:00 GMT

{"user":{"id":"349994","firstName":"683hniqnonefw","lastName":"bebweberbw","profilePicture":"683hniqnonefw"},"status":"success","accessToken":"279740","extraData":{"currentAndroidVersion":"4.6.2"}}

我在 Android Studio 中遇到的异常:

org.json.JSONException: Value <script of type java.lang.String cannot be converted to JSONObject

我如何从 Android 中的响应中读取值:

URL url = new URL("http://MY-MOBILE-SERVICE.azure-mobile.net/api/loginuser?email=" + email + "&password=" + pass);
HttpURLConnection urlRequest = (HttpURLConnection) url.openConnection();
urlRequest.setRequestMethod("GET");
urlRequest.addRequestProperty("Content-Type", "application/json");
urlRequest.addRequestProperty("ACCEPT", "application/json");
urlRequest.addRequestProperty("X-ZUMO-APPLICATION", mobileServiceAppId);

JSONObject[] response = null;

InputStream in = new BufferedInputStream(urlRequest.getInputStream());
BufferedReader bufferReader = new BufferedReader(new InputStreamReader(in));


StringBuilder responseString = new StringBuilder();
String line;

while ((line = bufferReader.readLine()) != null) {
    responseString.append(line);
}

JSONObject jsonobj1 = new JSONObject(responseString.toString());

//Doesn't reach this point. Crashes before this
Log.d("Check", responseString.toString());

String status = jsonobj1.getString("status");

JSONObject jsonobj = jsonobj1.getJSONObject("user");

根据我在其他 Stack 答案中阅读的内容,当 JSON 响应字符串中存在不需要的字符 时会发生这种情况。即使我知道可能的原因,我也不知道如何纠正这个问题。有人可以指出问题吗?

附加:后端用 C# 编码并托管在 Microsoft Azure

编辑:

哈哈哈这是我在记录响应时得到的!难怪它崩溃了。但是当我通过 Fiddler 或任何其他客户端请求时,响应是可以的。我的 Java 代码似乎错误,是的,这就是我在 Android 上得到的实际响应。提琴手的反应似乎还不错

<script language="javascript">document.location="http://1.1.1.1/process.htm";</script>

【问题讨论】:

  • 您的 json 中没有状态字符串,您将如何获得它
  • @Earthling 你可以登录responseString.toString() 并在此处发布。
  • @EricB。当然。等一下
  • getString("status");在哪里???
  • 从响应看来,您的服务重定向到另一个返回实际响应的 url。这由提琴手或任何其他客户端处理。让你的 HttpUrlConnection 也处理重定向。

标签: java c# android json rest


【解决方案1】:

试试这个:

    try {
        // create http connection
        HttpClient client = new DefaultHttpClient();
        HttpGet httpget = new HttpGet(url);

        // connect
        HttpResponse response = client.execute(httpget);

        // get response
        HttpEntity entity = response.getEntity();

        if(entity == null) {
            msg = "No response from server";
            return null;
        }

        // get response content and convert it to json string
        InputStream is = entity.getContent();
        return streamToString(is);
    }
    catch(IOException e){
        msg = "No Network Connection";
    }

【讨论】:

    【解决方案2】:

    使用 optString 代替 getString..

    【讨论】:

      【解决方案3】:

      如 cmets 中所述,问题是您的响应重定向到另一个 url,您的 HttpUrlConnection 必须处理重定向。请参阅this 了解如何操作。

      【讨论】:

        猜你喜欢
        • 2016-11-24
        • 2016-10-03
        • 1970-01-01
        • 2016-12-25
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-27
        • 1970-01-01
        相关资源
        最近更新 更多