使用JSONObject

时间:2017-03-26 05:42:32

标签: android json android-intent android-broadcastreceiver

我正在尝试解析BroadcastReceiver中的JSONObject,我的应用程序一直在向我显示此错误 错误接收广播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);
  }
  }
}

1 个答案:

答案 0 :(得分:0)

我不知道你什么时候发送你的广播,但问题出在这里:当你打电话给getJSON时,你告诉android运行你{{1}的一个实例在另一个线程中。所以他执行你的

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