xxxigdgrn 2016-11-17 08:13 采纳率: 0%
浏览 1928

Error parsing data org.json.JSONException:

Android环境是wampserver+eclipse,在网上找了个demo,想实现初步的注册和登录功能.但是测试的时候报了Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject,卡住了。求解...

logcat打印的信息:

 11-17 15:40:04.666: D/Log_Debug(14690): this json<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN""http://www.w3.org/TR/html4/strict.dtd">
11-17 15:40:04.666: D/Log_Debug(14690): <HTML><HEAD><TITLE>Bad Request</TITLE>
11-17 15:40:04.666: D/Log_Debug(14690): <META HTTP-EQUIV="Content-Type" Content="text/html; charset=us-ascii"></HEAD>
11-17 15:40:04.666: D/Log_Debug(14690): <BODY><h2>Bad Request - Invalid Header</h2>
11-17 15:40:04.666: D/Log_Debug(14690): <hr><p>HTTP Error 400. The request has an invalid header name.</p>
11-17 15:40:04.666: D/Log_Debug(14690): </BODY></HTML>
11-17 15:40:04.669: E/JSON Parser(14690): Error parsing data org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
11-17 15:40:04.671: W/System.err(14690): java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String org.json.JSONObject.getString(java.lang.String)' on a null object reference



  • 写回答

2条回答 默认 最新

  • xxxigdgrn 2016-11-17 08:15
    关注

    JSONParser.java

     package com.example.travelguide;
    import java.io.BufferedReader;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.InputStreamReader;
    import java.io.UnsupportedEncodingException;
    import java.util.List;
    
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.ClientProtocolException;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.client.utils.URLEncodedUtils;
    import org.apache.http.entity.StringEntity;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicHeader;
    import org.apache.http.protocol.HTTP;
    import org.json.JSONException;
    import org.json.JSONObject;
    
    import android.util.Log;
    public class JSONParser {
        static InputStream is=null;
        static JSONObject jObj=null;
        static String json="";
        //constructor
        public JSONParser()
        {}
    
        public JSONObject makeHttpRequest(String url,String method,List<NameValuePair> params)
        {
            try{
                // check for request method
                if(method.equals("POST")){
                    // request method is POST
                    // defaultHttpClient
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    HttpPost httpPost = new HttpPost(url);
                    //httpPost.setEntity(new UrlEncodedFormEntity(params));
    
                    //httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));//设置编码
                    StringEntity se;
                    se = new StringEntity(json.toString());
                    httpPost.setEntity(se);
                    se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));  
    
    
                    HttpResponse httpResponse = httpClient.execute(httpPost);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                    //Log.e("Log_tag", "POST url is "+url.toString());
                }else if(method.equals("GET")){
                    // request method is GET
                    DefaultHttpClient httpClient = new DefaultHttpClient();
                    String paramString = URLEncodedUtils.format(params, "gb2312");
                    url += "?"+ paramString;
                    HttpGet httpGet = new HttpGet(url);
                    HttpResponse httpResponse = httpClient.execute(httpGet);
                    HttpEntity httpEntity = httpResponse.getEntity();
                    is = httpEntity.getContent();
                    //Log.e("Log_tag", " GET url is"+url.toString());
                }
                else
                {
                    Log.e("Log_tag", "Params Error");
                }
            } 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);
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "UTF-8"));
                StringBuilder sb = new StringBuilder();
                String line = null;
                while((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                   // Log.d("log_tag", "line is :"+line+sb.toString());
                }
    
                is.close();
                json = sb.toString();
    //            Log.e("Log_tag", "this json is "+json.toString());
            } catch(Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
                Log.d("json", json.toString());//李霜新加的
    
            }
    
            // try parse the string to a JSON object
            try{
                jObj = new JSONObject(json);
    
            } catch(JSONException e) {
                Log.d("Log_Debug", "this json"+json.toString());
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
            return  jObj;
        }
    
    
    
    }
        /*static InputStream is = null;
        static JSONObject jObj = null;
        static String json = "";
    
        // constructor
        public JSONParser() {
        }
    
        // function get json from url
        // by making HTTP POST
        public JSONObject makeHttpRequest(String url, String method,
                List<NameValuePair> params) {
    
            // Making HTTP request
            try {
                // request method is POST
                // defaultHttpClient
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpPost httpPost = new HttpPost(url);// 建立HttpPost对象
                httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));// 设置编码
    
                // 发送Post,并返回一个HttpResponse对象
                HttpResponse httpResponse = httpClient.execute(httpPost);
                HttpEntity httpEntity = httpResponse.getEntity();
                is = httpEntity.getContent();// Creates a new InputStream object of
                                                // the entity.
    
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            } catch (ClientProtocolException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            try {
    
                BufferedReader reader = new BufferedReader(new InputStreamReader(
                        is, "UTF-8"));
    
                StringBuilder sb = new StringBuilder();
    
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                is.close();
                json = sb.toString();
            } catch (Exception e) {
                Log.e("Buffer Error", "Error converting result " + e.toString());
                Log.d("json", json.toString());
            }
    
            // try parse the string to a JSON object
            try {
                jObj = new JSONObject(json);
            } catch (JSONException e) {
                Log.e("JSON Parser", "Error parsing data " + e.toString());
            }
    
            // return JSON String
            return jObj;
    
        }*/
    
    
    评论

报告相同问题?

悬赏问题

  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 路易威登官网 里边的参数逆向
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入