我正在尝试从服务器解析我的JSON
响应,当我只使用响应生成System.out.println()
时这似乎很好,但是当我尝试解析它时,我得到JSONException: End of input at character 0;
从我读过的内容是因为我试图将数组解析为对象或反之,但我似乎无法弄清楚我哪里出错了,一些帮助或指导我会非常感激!
public static void main(String[] args) throws Exception {
HttpURLConnectionExample http = new HttpURLConnectionExample();
System.out.println("Testing 1 - Send Http GET request");
http.sendGet();
}
private void sendGet() throws Exception {
String url = "myUrl";
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("User-Agent", USER_AGENT);
con.setRequestProperty("api-key");
con.setRequestProperty("api-code");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
if (responseCode == 200) {
InputStream inputStr = con.getInputStream();
String encoding = con.getContentEncoding() == null ? "UTF-8" : con.getContentEncoding();
}
BufferedReader in = new BufferedReader(new InputStreamReader(con.getInputStream()));
String inputLine;
StringBuffer response = new StringBuffer();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
System.out.println("Body: " + response);
这是我从服务器得到的响应,看起来很好:
{"id":"16165","sensorid":"cc3200_8A7F30","pm1":"0.22","pm25":"0.23","pm10":"0.41","timestamp":"2018-01-30 12:28:56.000"},{"id":"16166","sensorid":"cc3200_E271A6","pm1":"0","pm25":"0.02","pm10":"0.46","timestamp":"2018-01-30 12:30:15.000"},{"id":"16167","sensorid":"cc3200_8A7F30","pm1":"0.09","pm25":"0.09","pm10":"0.58","timestamp":"2018-01-30 12:30:56.000"},{"id":"16168","sensorid":"cc3200_E271A6","pm1":"0.07","pm25":"0.07","pm10":"0.26","timestamp":"2018-01-30 12:32:15.000"}
这是我的JSONParse类:
@Override
protected Void doInBackground(Void... voids) {
HttpHandler sh = new HttpHandler();
String jsonString = sh.makeServiceCall(url);
Log.e(TAG, "Response from url: " + jsonString);
System.out.println("jsonstring" + jsonString.toString());
if (jsonString != null) {
try {
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray particles = jsonObject.getJSONArray("particles");
for (int i = 0; i < particles.length(); i++) {
JSONObject c = particles.getJSONObject(i);
String id = c.getString("id");
String sensorid = c.getString("sensorid");
String pm1 = c.getString("pm1");
String pm25 = c.getString("pm25");
String pm10 = c.getString("pm10");
String timestamp = c.getString("timestamp");
HashMap<String, String> particle = new HashMap<>();
particle.put("id", id);
particle.put("sensorid", sensorid);
particle.put("pm1", pm1);
particle.put("pm25", pm25);
particle.put("pm10", pm10);
particle.put("timestamp", timestamp);
}
}catch (final JSONException e) {
Log.e(TAG, "Json Parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Json Parsing error: " + e.getMessage(), Toast.LENGTH_LONG).show();
}
});
}
}else {
Log.e(TAG, "Couldn't get json from server.");
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(), "Couldn't get json from server. Check LogCat for possible errors!",
Toast.LENGTH_LONG)
.show();
}
});
}
return null;
答案 0 :(得分:0)
如果响应无法从服务器更改,则只需在响应的开始和结束时添加括号,并使用下面的代码来解析数据。
String jsonString = sh.makeServiceCall(url);
jsonString = "["+jsonString+"]";
JSONArray particles = new JSONArray(jsonString);
for (int i = 0; i < particles.length(); i++) {
JSONObject c = particles.getJSONObject(i);
String id = c.getString("id");
String sensorid = c.getString("sensorid");
String pm1 = c.getString("pm1");
String pm25 = c.getString("pm25");
String pm10 = c.getString("pm10");
String timestamp = c.getString("timestamp");
HashMap<String, String> particle = new HashMap<>();
particle.put("id", id);
particle.put("sensorid", sensorid);
particle.put("pm1", pm1);
particle.put("pm25", pm25);
particle.put("pm10", pm10);
particle.put("timestamp", timestamp);
}
答案 1 :(得分:0)
试试这个
jsonString = "{ \"particles\":[" + jsonString + "] }" ;
像这样使用
try {
jsonString = "{ \"particles\":[" + jsonString + "] }" ;
JSONObject jsonObject = new JSONObject(jsonString);
JSONArray particles = jsonObject.getJSONArray("particles");
for (int i = 0; i < particles.length(); i++) {
JSONObject c = particles.getJSONObject(i);
String id = c.getString("id");
String sensorid = c.getString("sensorid");
String pm1 = c.getString("pm1");
String pm25 = c.getString("pm25");
String pm10 = c.getString("pm10");
String timestamp = c.getString("timestamp");
HashMap<String, String> particle = new HashMap<>();
particle.put("id", id);
particle.put("sensorid", sensorid);
particle.put("pm1", pm1);
particle.put("pm25", pm25);
particle.put("pm10", pm10);
particle.put("timestamp", timestamp);
Log.i("TAG Id", ":" + id);
Log.i("TAG sensorid", ":" + sensorid);
Log.i("TAG pm1", ":" + pm1);
Log.i("TAG pm25", ":" + pm25);
Log.i("TAG pm10", ":" + pm10);
Log.i("TAG timestamp", ":" + timestamp);
}
}
输出日志