2016-08-18 154 views
0

我在android项目上工作在调用api时,抛出异常如下。我认为所有事情都做得正确,但太久却厌倦了这个例外。错误:<!java.lang.String类型的DOCTYPE无法转换为JSONObject

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

这里我的代码

public class APIConnectionManager { 

    private static int CONNECTION_TIMEOUT = 60000; 
    static JSONObject response = null; 
    public static final int GET = 1; 
    public static final int POST = 2; 


    /** 
    * Function to place a get call to the customer profile API that returns a JSONObject 
    * 
    * @param url  - The path to the resource 
    * @param params - A list of name value pairs 
    * @return - JSONObject 
    */ 
    public JSONObject doCustomerAPIGet(String url, int method, List<NameValuePair> params) { 

     try{ 

      // http client 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpEntity httpEntity = null; 
      HttpResponse httpResponse = null; 

      // check the http method request type 
      if(method==POST){ 

       HttpPost httpPost = new HttpPost(url); 

       // adding post params 
       if(params!=null){ 

        httpPost.setEntity(new UrlEncodedFormEntity(params)); 
       } 

       httpResponse = httpClient.execute(httpPost); 
      }else if (method==GET){ 

       // appending params to the url 
       if (params!=null) { 
        String paramString = URLEncodedUtils.format(params, "utf-8"); 

        url += "?" + paramString; 
       } 

       HttpGet httpGet = new HttpGet(url); 

       httpResponse = httpClient.execute(httpGet); 
      } 

      httpEntity = httpResponse.getEntity(); 

      String result = EntityUtils.toString(httpEntity); 

      response = new JSONObject(result); 


     }catch (UnsupportedEncodingException e){ 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (JSONException e){ 
      e.printStackTrace(); 
     } 

     return response; 

    } 
} 

错误在该行

response = new JSONObject(result); 

指向同样我得到响应

{"data":{"customerno":1339451,"loyalty_id":"9700354522","firstname":"raju","lastname":"king","email":"[email protected]","mobile":"9700354522","address":"1st street","city":"chennai","pincode":"600028","links":[]},"status":"success"} 

有人可以请告知这一点。

+0

请咨询您的后端开发人员 – Nilabja

+0

请查看我的回复以上 – Raju

+1

'<!DOCTYPE'可能表示您正在获取HTML而不是'JSON'。以调试模式运行以验证您得到的响应。 – Vucko

回答

1

您没有得到可以转换为JSON的响应。 您的API响应中存在错误,因此只需调试您的应用并向您的API开发人员显示错误。

+0

感谢你的建议我在做并得到了主意 – Raju

+0

@Raju Subramanian你欢迎 – Nikhil

相关问题