Android 接收带有JSONObject的广播意图{act=location_update flg=0x10(有附加项)}时出错

Android 接收带有JSONObject的广播意图{act=location_update flg=0x10(有附加项)}时出错,android,json,android-intent,android-broadcastreceiver,Android,Json,Android Intent,Android Broadcastreceiver,我正试图解析BroadcastReceiver中的一个JSONObject,我的应用程序不断向我显示这个错误接收广播意图时出错{act=location\u update flg=0x10(has extras)}第122行(JSONObject JSONObject=new JSONObject(json_字符串);) 不过,当我删除所有解析部分时,一切都很顺利。 以下是必要的代码: @Override protected void onResume() { super.onResum

我正试图解析BroadcastReceiver中的一个JSONObject,我的应用程序不断向我显示这个错误接收广播意图时出错{act=location\u update flg=0x10(has extras)}第122行(JSONObject JSONObject=new JSONObject(json_字符串);) 不过,当我删除所有解析部分时,一切都很顺利。 以下是必要的代码:

@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);
  }
  }
}
@覆盖
受保护的void onResume(){
super.onResume();
if(broadcastReceiver==null){
broadcastReceiver=新的broadcastReceiver(){
@凌驾
公共void onReceive(上下文、意图){
试一试{
getJSON();
JSONObject JSONObject=新的JSONObject(json_字符串);
JSONArray JSONArray=jsonObject.getJSONArray(“响应”);
int i=0;
双纬度,经度;

虽然(我不知道你什么时候发送广播,但问题是:当你调用你的
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();
    }

}
@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 "));
}

getJSON调用创建了一个新任务,该任务将在另一个线程上运行
json\u string=null
。你能在你的
jsonObject=new…
上面加一个if检查吗?好的,我现在就试试。不,实际上它显示了一个错误。请发布完整的堆栈跟踪。没问题,我会发布。我会定期(每5秒)从GPS\u服务发送广播,你能解释一下我应该做什么吗!我用一个可能的解决方案编辑了我的答案,但你需要了解的是你的
getJSON
是异步的,所以在你调用它之后,你就不会有一个有效的
json\u字符串,因为你的任务是在后台执行的。我按照你说的做了,先生,应用程序不再挤压,但仍然没有做它应该做的事情(地图上没有标记)再次编辑,你的标记放置逻辑是正确的,只有你的JSON解析应该在
onPostExecute
中完成。我们就快到了!谢谢,我会尝试的