带有AsyncTask的Android Studio ResponseListener

时间:2016-09-07 09:14:17

标签: android json google-maps google-maps-api-3 android-asynctask

我一直在询问有关这个特定代码的很多问题,但似乎每当我修复某些内容时,我都会遇到新的错误,所以请原谅,我还是Android的新手工作室。

我试图使用JSON解析(使用php脚本查询)从MySQL数据库解析几个双坐标,然后将这些双精度值作为标记放在谷歌地图中。

现在的问题是,即使我正确地解析了双值,似乎ResponseListener在主线程上做了太多的工作(根据我以前的一个问题的回答,不应该' t是一个问题)。

我目前正在尝试解析7个标记,其中包含marker_id,纬度和经度值以及double和片段整数。

我得到了这个"错误"在logcat:

I/Choreographer: Skipped 33 frames!  The application may be doing too much work on its main thread.

我100%知道它是无法运行的响应者,因为代码运行良好,没有。

这是我的响应列表的代码,我试图使用AsyncTask在backgroundthread上运行它,无论是否同时执行都会给我相同的错误,这告诉我,我使用的AsyncTask错误,或者你不能在AsyncTask中使用ResponseListener:

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

    private ProgressDialog pDialog;

    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(AllMarkersActivity.this);
        pDialog.setMessage("Loading markers...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }

    protected String doInBackground(String... params) {

        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

        Response.Listener<String> responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {

                    JSONObject jsonObject = new JSONObject(response);
                    Iterator<String> keys = jsonObject.keys();
                    while( keys.hasNext() )
                    {
                        String key = keys.next();
                        if(!key.equals("success")){   // or use this if your keys are always in digits form.. if(TextUtils.isDigitsOnly(key))
                            Log.v("category key", key);

                            JSONObject innerJObject = jsonObject.getJSONObject(key);
                            double latV = 1.1, lngV = 1.1;

                            String lat_str = innerJObject.getString("lat");
                            String lng_str = innerJObject.getString("lng");

                            if(!TextUtils.isEmpty(lat_str) && TextUtils.isDigitsOnly(lat_str))
                            latV = Double.parseDouble(lat_str);
                            if(!TextUtils.isEmpty(lng_str) && TextUtils.isDigitsOnly(lng_str))
                            lngV = Double.parseDouble(lng_str);

                            Log.i("LAT:", latV + "");
                            Log.i("LAT:", lngV + "");

                            //Random cord = new Random();

                            //double LATrangeMin = 55.6;
                            //double LATrangeMax = 55.7;

                            //double LNGrangeMin = 12.4;
                            //double LNGrangeMax = 12.7;


                            //double lat = LATrangeMin + (LATrangeMax - LATrangeMin) * cord.nextDouble();
                            //double lng = LNGrangeMin + (LNGrangeMax - LNGrangeMin) * cord.nextDouble();

                            LatLng Mplace = new LatLng(latV, lngV);

                            MarkerOptions marker = new MarkerOptions().position(Mplace).title("Marker").snippet("Snippet:" + "0");
                            mMap.addMarker(marker);

                        }
                    }





                } catch (JSONException e) {
                    e.printStackTrace();
                    Log.i("RESPONSE:", "[" + response + "]");
                    Toast.makeText(getApplicationContext(),
                            "Error: " + e.getMessage(),
                            Toast.LENGTH_LONG).show();
                }

            }

        };

        MarkerRequest markerRequest = new MarkerRequest(responseListener);
        RequestQueue queue = Volley.newRequestQueue(AllMarkersActivity.this);
        queue.add(markerRequest);


        return null;
    }



    protected void onPostExecute(String file_url) {
        // dismiss the dialog once got all details
        pDialog.dismiss();
    }


}

在我正在显示的代码中,我正在测试我收到的双值。假设double值已从数据库lat_str解析为String,然后解析为LatV为double。但是这种情况从未发生过,而logcat则给出了这些值:

V/category key: 0
I/LAT:: 1.1
I/LNG:: 1.1
V/category key: 1
I/LAT:: 1.1
I/LNG:: 1.1
V/category key: 2
I/LAT:: 1.1
I/LNG:: 1.1
V/category key: 3
I/LAT:: 1.1
I/LNG:: 1.1

所有7个键。

这在开始时成功地为Latitude 1.1和Longitude 1.1放置了7个标记,因此为了确认它检索7个标记,我尝试使用随机的lat和lng值,这些值成功地放置了7个标记。

问题在于解析纬度和经度。我不确定双值是否被解析错误,或者主线程是否真的被重载。

当我记录innerObject时,我得到了这个:

I/innerObject:: [{"success":false,"marker_id":null,"lat":null,"lng":null,"snippet":null}]

有没有人知道更好的方法,或者我使用AsyncTask的方式有问题?

我也在使用Thread.sleep();确保AsyncTask甚至可以运行,如果我不这样做,那么progressdialog甚至都不会出现。

请回答一个例子,因为我还不熟悉Android Studio:)

感谢您的帮助! 非常赞赏!

调试日志(我认为):

09-07 12:26:31.429 16783-17146/? I/msm8974_platform:          platform_send_audio_calibration: sending audio calibration for snd_device(2)     acdb_id(15)
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB -> send_audio_cal,     acdb_id = 15, path =  0
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB -> send_adm_topology
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB ->     ACDB_CMD_GET_AUDPROC_COMMON_TOPOLOGY_ID
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB -> send_asm_topology
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB ->     ACDB_CMD_GET_AUDPROC_STREAM_TOPOLOGY_ID
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB -> send_audtable
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB ->     ACDB_CMD_GET_AUDPROC_COMMON_TABLE
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB ->     AUDIO_SET_AUDPROC_CAL
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB -> send_audvoltable
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB ->     ACDB_CMD_GET_AUDPROC_GAIN_DEP_STEP_TABLE


                                            [ 09-07 12:26:31.429 16783:17146 D/         ]
                                            Failed to fetch the lookup information of the device 0000000F 
09-07 12:26:31.429 16783-17146/? E/ACDB-LOADER: Error: ACDB AudProc vol returned = -19
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB -> AUDIO_SET_AUDPROC_VOL_CAL
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB -> send_afe_cal
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB -> ACDB_CMD_GET_AFE_COMMON_TABLE


                                            [ 09-07 12:26:31.429 16783:17146 D/         ]
                                            Failed to fetch the lookup information of the device 0000000F 
09-07 12:26:31.429 16783-17146/? E/ACDB-LOADER: Error: ACDB AFE returned = -19
09-07 12:26:31.429 16783-17146/? D/ACDB-LOADER: ACDB -> AUDIO_SET_AFE_CAL
09-07 12:26:31.435 16783-17146/? D/audio_hw_primary: select_devices: done
09-07 12:26:31.428 17302-17302/? W/Binder_3: type=1400 audit(0.0:22441): avc: denied { ioctl } for path="socket:[6346182]" dev="sockfs" ino=6346182 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
09-07 12:26:31.428 17302-17302/? W/Binder_3: type=1400 audit(0.0:22442): avc: denied { ioctl } for path="socket:[6346182]" dev="sockfs" ino=6346182 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
09-07 12:26:31.464 23751-23751/com.apppack.myapp
I/Google Maps Android API: Google Play services package version: 9452438
09-07 12:26:31.508 18370-18370/? W/Binder_F: type=1400 audit(0.0:22443): avc: denied { ioctl } for path="socket:[6346276]" dev="sockfs" ino=6346276 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
09-07 12:26:31.508 18370-18370/? W/Binder_F: type=1400 audit(0.0:22444): avc: denied { ioctl } for path="socket:[6346276]" dev="sockfs" ino=6346276 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
09-07 12:26:31.528 17302-17302/? W/Binder_3: type=1400 audit(0.0:22445): avc: denied { ioctl } for path="socket:[6344011]" dev="sockfs" ino=6344011 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
09-07 12:26:31.528 17302-17302/? W/Binder_3: type=1400 audit(0.0:22446): avc: denied { ioctl } for path="socket:[6344011]" dev="sockfs" ino=6344011 ioctlcmd=7704 scontext=u:r:system_server:s0 tcontext=u:r:system_server:s0 tclass=unix_stream_socket permissive=0
09-07 12:26:31.544 16985-17019/? I/ActivityManager: Displayed com.apppack.myapp/.AllMarkersActivity: +97ms
09-07 12:26:31.595 23751-24512/com.apppack.myapp W/Google Maps Android API: GLHudOverlay deprecated; draw(): no-op

0 个答案:

没有答案