如何解决此错误:org.json.JSONException:字符0的预期文字值

时间:2019-06-25 14:06:20

标签: android json

我正在使用来自openweathermap.org的api构建天气应用,当我运行该程序时显示错误,如何解决?

我已经在openweathermap.org上创建了一个帐户来创建api密钥,并使用url =“ https://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=2ee7238c0e02a5b1efef8418a075c72c”

public class MainActivity extends AppCompatActivity {

    public class DownloadTask extends AsyncTask<String,Void,String>{

        @Override
        protected String doInBackground(String... urls) {
            URL url;
            HttpURLConnection conn=null;
            try {
                String result="";
                url=new URL(urls[0]);
                conn=(HttpURLConnection) url.openConnection();
                conn.connect();
                InputStream is=conn.getInputStream();
                InputStreamReader reader=new InputStreamReader(is);
                int data=reader.read();
                while(data!=-1){
                    char current=(char)data;
                    result=current+result;
                    data=reader.read();
                }
                return result;

            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);

            try {
                JSONObject json=new JSONObject(result);
                String weatherInfo=json.getString("weather");
                Log.i("Contents ",weatherInfo);
                JSONArray arr=new JSONArray(weatherInfo);

                for(int i=0;i<arr.length();i++){
                    JSONObject jsonPart=arr.getJSONObject(i);
                    Log.i("main",jsonPart.getString("main"));
                    Log.i("decription",jsonPart.getString("description"));

                }

            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        DownloadTask task=new DownloadTask();
        try {
            task.execute("https://api.openweathermap.org/data/2.5/weather?q=London,uk&APPID=2ee7238c0e02a5b1efef8418a075c72c").get();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

输出为:

org.json.JSONException: Expected literal value at character 0 of }002:"doc","nodnoL":"eman",3473462:"di",0063:"enozemit",}2114941651:"tesnus",8424341651:"esirnus","BG":"yrtnuoc",1900.0:"egassem",4141:"di",1:"epyt"{:"sys",1880741651:"td",}09:"lla"{:"sduolc",}033:"ged",1.3:"deeps"{:"dniw",00001:"ytilibisiv",}62.992:"xam_pmet",51.092:"nim_pmet",37:"ytidimuh",8101:"erusserp",24.592:"pmet"{:"niam","snoitats":"esab",]}"d01":"noci","niar thgil":"noitpircsed","niaR":"niam",005:"di"{[:"rehtaew",}15.15:"tal",31.0-:"nol"{:"drooc"{

2 个答案:

答案 0 :(得分:2)

这是您的问题:

InputStream is=conn.getInputStream();
InputStreamReader reader=new InputStreamReader(is);
int data=reader.read();
while(data!=-1){
    char current=(char)data;
    result=current+result;    // <<==== HERE
    data=reader.read();
}

您的代码将JSON以相反的顺序存储在result 中! 自然,反向JSON是不可解析的。

要解决此问题,请更改:

 result = current + result;

 result = result + current;

或更妙的是,使用https://www.baeldung.com/java-convert-reader-to-string

中所述的“批量读取”选项之一

(至少将Reader包裹在BufferedReader中。一次从无缓冲的输入管道读取一个字符或字节效率很低。)

答案 1 :(得分:1)

您尝试解析/读取的对象不是以字符“ {”开头。并且JSON对象/文件始终以“ {”

开头
result=current+result; // this line should be replaced
result += current; //-----by this line