Android的错误 - 收到的HTML而不是JSON的 [英] Error Android - received HTML instead of JSON

查看:107
本文介绍了Android的错误 - 收到的HTML而不是JSON的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个的Andr​​oid 应用程序,我试图让 JSON 从配置为返回一个JSON对象(服务器响应.. ./current_user.json)接收当获取消息,但我得到的答案是的 HTML 格式,而不是在 JSON 格式预期

我不明白为什么会这样,因为我做的浏览器,并与该程序的 RESTClient实现相同的请求,并得到了在 JSON 格式正确的答案。

下面是code我使用。

  JSONObject的JSON =新的JSONObject();            的HttpParams PARAMS =新BasicHttpParams();
            HttpConnectionParams.setSoTimeout(PARAMS,10000);
            HttpClient的HttpClient的=新DefaultHttpClient(PARAMS);
            HTTPGET GET =新HTTPGET(url_getiduser);            HTT presponse响应= httpClient.execute(获取);
            字符串sresponse =错误;            Log.d(URL得到,url_getiduser);
            Log.d(pedido GET,get.getMethod());
            如果(响应!= NULL)
            {
                。InputStream的时间= response.getEntity()的getContent();
                sresponse = convertStreamToString(上);                Log.d(resposta HTTP,sresponse);
                如果(!sresponse.equals(错误))
                {                    的JSONObject对象=新的JSONObject(sresponse);
                    id_user =(字符串)object.get(ID);                    JSON =对象;
                    Log.d(objecto JSON,object.toString());                }
                其他Log.d(,sresponse关于JSON解析器错误);


解决方案

有你在哪里得到的 HTML文本

少数病例

  1. 您可能已经调用了错误的函数,它给出了一个404页。


  2. 可能在服务器端数据库错误,你会得到的数据库错误的消息


  3. 服务器可能会传递具有风格数据的 HTML标记


但你最好记录响应,并将其粘贴在这里。

I'm working on a Android application and I'm trying to get a JSON response from a server which is configured to return a json object (".../current_user.json") when receives a GET message, but the answer I get is in HTML format and not in JSON format as expected.

I don't understand why is this happening because I did the same requests on the browser and with the program RESTClient and got the right answer in JSON format.

Here is the code I'm using.

        JSONObject json = new JSONObject();

            HttpParams params = new BasicHttpParams();
            HttpConnectionParams.setSoTimeout(params, 10000);
            HttpClient httpClient = new DefaultHttpClient(params);
            HttpGet get = new HttpGet(url_getiduser);

            HttpResponse response = httpClient.execute(get);                 
            String sresponse = "error";

            Log.d("url get", url_getiduser);
            Log.d("pedido get", get.getMethod());
            if(response != null)
            {
                InputStream in = response.getEntity().getContent();
                sresponse = convertStreamToString(in);

                Log.d("resposta http", sresponse);
                if(!sresponse.equals("error"))
                {

                    JSONObject object = new JSONObject(sresponse);
                    id_user = (String) object.get("id");

                    json = object;
                    Log.d("objecto json", object.toString());

                }
                else Log.d("Error on json parser", sresponse);

解决方案

There are few cases where you get HTML text

  1. You might have called a wrong function which gives a 404 page.

  2. Might be a database error on server side where you will get database error message

  3. Server might be sending a styled data which has HTML tags

But you better Log the response and paste it here.

这篇关于Android的错误 - 收到的HTML而不是JSON的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆