为什么我会收到Json格式错误的异常

时间:2018-07-12 19:14:50

标签: java android arrays json api

我有一个Android应用程序,该应用程序在后台执行一些网络处理,从https://ipinfo.io/json处的JSON API请求信息。 bufferReader httpConnection已成功制作,BufferReader包含了我稍后添加到输出String(while循环)中的信息。

现在转换为json对象时。

String INTERNET_IP_SOURCE = "https://ipinfo.io/json"

是全局声明的。我收到有关格式错误的json对象的异常。我在这里想念什么?谢谢!

private class TaskPublicNet extends AsyncTask<Void, String, Void> {
    String output = "";
    int progress = 0;
    String json = "";
    //only 7 lines of information for public ip info (set progress at 5)
    final static int PROGRESS_MAX = 5;
    String[] iface;

    @Override
    protected void onPreExecute() {
        progressBar.setMax(PROGRESS_MAX);
        progressBar.setVisibility(View.VISIBLE);
        progressBar.setProgress(1);
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        progressBar.setProgress(5);
        Log.v("ALL ", localIfaceInfo.toString());
    }

    @Override
    protected void onProgressUpdate(String... values) {
        Log.v("PROGRESS ", values[0]);
        localIfaceInfo.add(values[0]);
        localNetAdapter.notifyDataSetInvalidated();
    }

    @Override
    protected Void doInBackground(Void... params) {

        /** Getting public interface information*/
        try {
            // Make a URL to the web page
            URL url = new URL(INTERNET_IP_SOURCE);
            // Get the input stream through URL Connection
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = urlConnection.getInputStream();
            BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream));
            String line = "";
            // read each line and write to System.out
            while ((line = bf.readLine()) != null) {
                Log.v("LINE ", line);
                output += line;
            }
            bf.close();
        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        }

        //json parsing
        try {

            JSONArray ja = new JSONArray(output);
            JSONObject jo = (JSONObject) ja.get(0);
            String publicIp = jo.get("ip").toString();
            String city = jo.get("city").toString();
            String provider = jo.get("org").toString();
            Log.v("PUBLIC ", publicIp +" "+city+" "+provider);
            publishProgress("Public Ip: "+publicIp);
            publishProgress("City: "+city);
            publishProgress("Provider: "+provider);
        }
        catch (JSONException ex){
            ex.printStackTrace();
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:3)

响应看起来像这样

{
  "ip": "XXXXXX",
  "city": "",
  "region": "",
  "country": "XX",
  "loc": "XXXXX,XXXXX",
  "org": "XXXXXXXX"
}

但是在您的代码中,您正在这样做

 JSONArray ja = new JSONArray(output);
 JSONObject jo = (JSONObject) ja.get(0);

json是一个对象,不是数组。尝试直接将其解析为对象

JSONObject jo = new JSONObject(output)