我在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"}
有人可以請告知這一點。
請諮詢您的後端開發人員 – Nilabja
請查看我的回覆以上 – Raju
'<!DOCTYPE'可能表示您正在獲取HTML而不是'JSON'。以調試模式運行以驗證您得到的響應。 – Vucko