Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 获得;输入结束,字符位于0“;当试图解析我的JSON对象时_Java_Android_Json - Fatal编程技术网

Java 获得;输入结束,字符位于0“;当试图解析我的JSON对象时

Java 获得;输入结束,字符位于0“;当试图解析我的JSON对象时,java,android,json,Java,Android,Json,我正试图解析来自服务器的JSON响应,当我用响应生成System.out.println()时,这似乎很好,但当我尝试解析它时,我得到JSONException:输入结束于字符0 从我所读到的内容来看,这是因为我试图将数组解析为对象或相反,但我似乎无法找出哪里出了问题,请提供一些帮助或指导,非常感谢 public static void main(String[] args) throws Exception { HttpURLConnectionExample http = new

我正试图解析来自服务器的
JSON
响应,当我用响应生成
System.out.println()
时,这似乎很好,但当我尝试解析它时,我得到
JSONException:输入结束于字符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;
@覆盖
受保护的空位背景(空位…空位){
HttpHandler sh=新的HttpHandler();
字符串jsonString=sh.makeServiceCall(url);
Log.e(标签,“来自url的响应:”+jsonString);
System.out.println(“jsonstring”+jsonstring.toString());
if(jsonString!=null){
试一试{
JSONObject JSONObject=新的JSONObject(jsonString);
JSONArray particles=jsonObject.getJSONArray(“particles”);
对于(int i=0;i
若响应无法从服务器更改,则只需在响应的开始和结束处添加括号,并使用下面的代码解析数据

    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);
    }
String jsonString=sh.makeServiceCall(url);
jsonString=“[”+jsonString+“]”;
JSONArray粒子=新的JSONArray(jsonString);
对于(int i=0;i
试试这个

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);


        }
  }
试试看{
jsonString=“{\”粒子\:[“+jsonString+”]}”;
JSONObject JSONObject=新的JSONObject(jsonString);
JSONArray particles=jsonObject.getJSONArray(“particles”);
对于(int i=0;i
输出日志


服务器响应必须在数组内,因为存在多个对象。正如@NiranjPatel所说,服务器响应必须在具有关键“粒子”的数组内上面提供的json响应无效。哦,好的,我如何在数组中做出响应?@Kkona无法更改服务器的响应?好的,我尝试了这个,但得到了另一个错误,而不是我原来遇到的错误:json解析错误:值{“粒子”:[]}无法将类型为org.json.JSONObject的JSONArray@Kkona你把这行放在哪一个地方。@Kkona请检查我的编辑答案。开始时,就在第一个Log.e()之前,我应该把它放在其他地方吗?@Kkona它工作正常,我正在检查输出。