使用 JSONObject 接收广播 Intent { act=location_update flg=0x10 (has extras) } 时出错

Error receiving broadcast Intent { act=location_update flg=0x10 (has extras) } with JSONObject

我正在尝试解析 BroadcastReceiver 中的 JSONObject,但我的应用一直崩溃并向我显示此错误 Error receiving broadcast Intent { act=location_update flg= 0x10(有额外内容)} 第 122 行(JSONObject jsonObject= new JSONObject(json_string);) 虽然,当我删除所有解析部分时,一切顺利。 这是必要的代码:

@Override
protected void onResume() {
    super.onResume();
    if(broadcastReceiver == null){
        broadcastReceiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {

                try {
                    getJSON();
                    JSONObject jsonObject= new JSONObject(json_string);
                    JSONArray jsonArray = jsonObject.getJSONArray("response");

                    int i=0 ;
                    Double latitude, longitude;
                    while (i<jsonArray.length()){
                        JSONObject jo= jsonArray.getJSONObject(i);
                        latitude = jo.getDouble("latitude");
                        longitude = jo.getDouble("longitude");
                        map.addMarker(new MarkerOptions()
                                .position(new LatLng(latitude,longitude))
                                .icon(BitmapDescriptorFactory.fromResource(R.drawable.taxi))
                                .title("taxi"));
                        i++;
                } }catch (JSONException e) {
                    e.printStackTrace();
                }

                lngg= intent.getDoubleExtra("longitude",-1);
                latt= intent.getDoubleExtra("latitude",-1);

                map.addMarker(new MarkerOptions()
                        .position(new LatLng(latt,lngg))
                        .icon(BitmapDescriptorFactory.fromResource(R.drawable.locationpeople1))
                        .title("I am here "));
            }
        };
    }
    registerReceiver(broadcastReceiver, new IntentFilter("location_update"));

}

public void getJSON(){
new BackgroundTask().execute();

}


class BackgroundTask extends AsyncTask<Void,Void,String>{
    String JSON_STRING;
    String json_url ;
    @Override
    protected void onPreExecute() {

        json_url="http://frequentative-hicko.000webhostapp.com/A.php";
    }

    @Override
    protected String doInBackground(Void... voids) {

        try {
            URL url = new URL(json_url);
            HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
            InputStream inputStream = httpURLConnection.getInputStream();
            BufferedReader bufferedReader= new BufferedReader( new InputStreamReader(inputStream));
            StringBuilder stringBuilder= new StringBuilder();
            while( (JSON_STRING = bufferedReader.readLine()) != null ){

             stringBuilder.append(JSON_STRING+"\n");
            }
            bufferedReader.close();
            inputStream.close();
            httpURLConnection.disconnect();
            return stringBuilder.toString().trim();




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

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onProgressUpdate(values);
    }

    @Override
    protected void onPostExecute(String result) {
       // TextView textView =(TextView) findViewById(R.id.textView2);
       // textView.setText(result); //JSON Format
        json_string=result;
    }
}
public  void parseJSON(View view){
  if(json_string == null){
      Toast.makeText(getApplicationContext(),"Aucun taxi n'est disponible pour l'instant",Toast.LENGTH_LONG);
  }
  }
}

我不知道你什么时候发送你的广播,但问题是:当你调用你的 getJSON 时,你是在告诉 android 到 运行 你的 BackgroundTask 在另一个线程中。所以他执行了你的

JSONObject jsonObject= new JSONObject(json_string); // and all the rest

在您的 getJSON() 完成之前。请尝试将 getJSON() 之后的所有逻辑移至 onPostExecute()

像这样:

@Override
protected void onPostExecute(String result) {
   // This is called when you finish your background task
   // so here is the right place to parse your json response and add yout markers :)
    json_string=result;
    try {
        JSONObject jsonObject= new JSONObject(json_string);
        JSONArray jsonArray = jsonObject.getJSONArray("response");

        int i=0 ;
        Double latitude, longitude;
        while (i<jsonArray.length()){
            JSONObject jo= jsonArray.getJSONObject(i);
            latitude = jo.getDouble("latitude");
            longitude = jo.getDouble("longitude");
            map.addMarker(new MarkerOptions()
                    .position(new LatLng(latitude,longitude))
                    .icon(BitmapDescriptorFactory.fromResource(R.drawable.taxi))
                    .title("taxi"));
            i++;
        }

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

}

在你的 onReceive 中是这样的:

@Override
public void onReceive(Context context, Intent intent) {

    getJSON();
    lngg= intent.getDoubleExtra("longitude",-1);
    latt= intent.getDoubleExtra("latitude",-1);

    map.addMarker(new MarkerOptions()
            .position(new LatLng(latt,lngg))
            .icon(BitmapDescriptorFactory.fromResource(R.drawable.locationpeople1))
            .title("I am here "));
}