伙计们,你可以帮助我一点,我得到这个错误:
"JSONException: Value <!DOCTYPE of type java.lang cannot be converted to JSONObject"
Run Code Online (Sandbox Code Playgroud)
当我在解析数据时,这是我的代码:
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} 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);
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());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我实例化Parser的代码:
private void fillSpinnerCabTypes() {
List<String> cabTypesSpinner = new ArrayList<String>();
JSONParser jsonParser = new JSONParser();
JSONObject cabTypesObject = jsonParser.getJSONFromUrl(urlTypeCabs);
try{
TypesArray = cabTypesObject.getJSONArray(TAG_TYPES);
for(int i = 0; i < TypesArray.length(); i++){
JSONObject c = TypesArray.getJSONObject(i);
String name = c.getString(TAG_NAME);
cabTypesSpinner.add(name);
}
}catch(Exception e ){
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, cabTypesSpinner);
final Spinner spnCabTypes = (Spinner)findViewById(R.id.spnTypeOfCab);
adapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item);
spnCabTypes.setAdapter(adapter);
}
Run Code Online (Sandbox Code Playgroud)
我真的很困惑.我正在服务器中的Django后端数据库中填充微调器.
这是我的JSON数据
{"Types": [{"name": "Normal"}, {"name": "Discapacitados"}, {"name": "Buseta"}]}
Run Code Online (Sandbox Code Playgroud)
此问题来自服务器.
您请求的URL,向您发送数据,但不是JSON格式.
你得到的例外情况是告诉你服务器发送给你的字符串以:
<!DOCTYPE
Run Code Online (Sandbox Code Playgroud)
这可以是:
要进一步调试,只需json在logcat中打印变量的内容:
Log.d("Debug", json.toString());
jObj = new JSONObject(json);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10183 次 |
| 最近记录: |