【发布时间】:2018-01-15 22:09:24
【问题描述】:
我正在尝试检索服务器 json 数据。
我的代码: 主活动
String url = "http://........com/...../...."
String[] params = new String[]{url};
JSONParser jsonParser = new JSONParser();
try {
String response = jsonParser.execute(url).get();
JSONArray jsonArray = new JSONArray(response);
JSONObject jsonObje = (JSONObject) jsonArray.get(0);
}
JSONParser.JAVA
HttpURLConnection connection = null;
BufferedReader reader = null;
String responseText="";
try {
URL url = new URL(params[0]);
connection = (HttpURLConnection) url.openConnection();
connection.connect();
//GIVE ERROR THIS LINE!
InputStream stream = connection.getInputStream();
//ERROR LINE!
reader = new BufferedReader(new InputStreamReader(stream));
错误:org.json.JSONException:输入结束于字符 0
Json 文件:
{"PersonelID":2,"PersonelAdıSoyadı":"New Driver","PersonelTelefon":"","PersonelMail":"driver@mail.com","PersonelPassword":null,"PersonelTipi":1,"AracID":0,"SirketID":20,"SuccessCode":1}
【问题讨论】:
-
如果变量 "response" 的值包含此字符串 "{"PersonelID":2,"PersonelAdıSoyadı":"New Driver",..." 您会收到错误,因为这不是JSON 数组!不要使用
JSONArray,而是使用JSONObject -
能否向我们展示完整的例外情况?