“JSONException:获取数据时字符0的输入结束”

时间:2018-03-27 18:58:19

标签: java android json request

我正在尝试从Reviewmeta的API获取JSON数据 该请求回复了以下内容:

{
"rating":"3.9",
"s_overall":"3",
"href":"https:\/\/reviewmeta.com\/amazon\/SOMETEXTHERE",
"count":"1198"
}

当我试图获取计数值时,我只会收到此错误:

  

03-27 20:46:27.580 18567-18567 / at.mrminemeet.reviewcheck E / JSON EXEPTION:ERROR:org.json.JSONException:

的字符0输入结束

我正在使用它来获取带有ASYNC任务的JSON信息:

public class ReviewMetaDATA extends AsyncTask<String, Integer, String> {
    private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
    HttpURLConnection urlConnection;

    @Override
    protected void onPreExecute() {

        progressDialog.setMessage("Daten anfordern");
        progressDialog.show();
        progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
            public void onCancel(DialogInterface arg0) {
                ReviewMetaDATA.this.cancel(true);
            }
        });
    }

    @Override
    protected String doInBackground(String... url) {

        StringBuilder result = new StringBuilder();

        try {
            URL reviewmeta_url= new URL(url[0]);
            urlConnection = (HttpURLConnection) reviewmeta_url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

            BufferedReader reader = new BufferedReader(new InputStreamReader(in));

            String line;
            while ((line = reader.readLine()) != null) {
                result.append(line);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            urlConnection.disconnect();
        }
        Log.d("Info", result.toString());
        return result.toString();
    }

    @Override
    protected void onProgressUpdate(Integer... values) {

    }

    @Override
    protected void onPostExecute(String str) {

        try {

            JSONObject jresult = new JSONObject(str);

            int count = jresult.getInt("count");

            Log.d("REQUEST", String.valueOf(count));


            this.progressDialog.dismiss();


        } catch (JSONException e) {
            Log.e("JSON EXEPTION", "ERROR: " + e.toString());
            this.progressDialog.dismiss();
        }
    }
}

我认为问题出现在 onPostExecute funktion中的try-catch中,因为尝试的行将被执行。 我还查了一些其他帖子谈论这个,但我没弄清楚那么多。

任何解决这个问题的想法或我做错了什么?

2 个答案:

答案 0 :(得分:0)

你似乎得到一个空字符串作为json响应,它可能是由这个引起的:

URL reviewmeta_url= new URL(url[0]);
            urlConnection = (HttpURLConnection) reviewmeta_url.openConnection();
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

您忘记在连接中调用connect(),然后您最终捕获异常,这就是您获取空字符串的原因。试试这个:

URL reviewmeta_url= new URL(url[0]);
            urlConnection = (HttpURLConnection) reviewmeta_url.openConnection();
            urlConnection.connect(); <<forgot this
            InputStream in = new BufferedInputStream(urlConnection.getInputStream());

答案 1 :(得分:0)

所以我终于得到了答案。 我发现了自己最尴尬的问题以及其他一些导致问题的问题。

所以我最大的错误是,我以某种方式设法删除了Internet权限。 接下来的问题是我没有 .connect(); 所以感谢@Levi Albuquerque

感谢@Barns的提示,让它变得更好。

所以这是工作代码:

public class ReviewMetaDATA extends AsyncTask<String, Integer, String> {

        private ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
        HttpURLConnection urlConnection;

        @Override
        protected void onPreExecute() {

            progressDialog.setMessage("Daten anfordern");
            progressDialog.show();
            progressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
                public void onCancel(DialogInterface arg0) {
                    ReviewMetaDATA.this.cancel(true);
                }
            });
        }

        @Override
        protected String doInBackground(String... url) {
            StringBuilder result = new StringBuilder();

            try {
                URL reviewmeta_url = new URL(url[0]);
                urlConnection = (HttpURLConnection) reviewmeta_url.openConnection();

                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                InputStream in = new BufferedInputStream(urlConnection.getInputStream());

                BufferedReader reader = new BufferedReader(new InputStreamReader(in));

                String line;
                while ((line = reader.readLine()) != null) {
                    result.append(line);
                }

            } catch (Exception e) {
                Log.e("ERROR", "ERROR: " + e.toString());
            } finally {
                urlConnection.disconnect();
            }
            Log.d("Info", result.toString());
            return result.toString();
        }

        @Override
        protected void onProgressUpdate(Integer... values) {

        }

        @Override
        protected void onPostExecute(String str) {

            this.progressDialog.dismiss();

            // When str is not empty then do that code
            if(str != null && !str.isEmpty()) {
                try {

                    JSONObject jresult = new JSONObject(str);

                    Log.d("REQUEST", jresult.getString("count"));


                } catch (JSONException e) {
                    Log.e("JSON EXEPTION", "ERROR: " + e.toString());
                }

            }
            // else log it
            else{
                Log.e("Request", "String was empty");
            }
        }
    }

谢谢你的帮助。

对不起,我用它打扰了你。