如何访问void Async任务

时间:2014-08-05 08:44:36

标签: java android

我不熟悉Android& Java和我想从void Asynctask访问并从中获取价值:

这是Asynctask的整个代码,我想传递lan&的值。从onPost执行lon到另一个活动 编辑

public static void mapView(final String serial, final String username,
        final String password, final String carname, final Context c) {

     class HttpGetAsyncTask extends AsyncTask<String, Void, String> {
        String paramUsername = username;
        String paramPassword = password;
        String paramSerial = serial;
        String paramcarname = carname;

        @Override
        public String doInBackground(String... params) {

            try {
                URL u = new URL(
                        "http://***********/software/AndroidControl.aspx?UserName="
                                + paramUsername + "&PassWord="
                                + paramPassword + "&Action=UnitLocation"
                                + "&SerialNumber=" + paramSerial);
                HttpURLConnection conn = (HttpURLConnection) u
                        .openConnection();
                conn.setRequestMethod("GET");
                conn.connect();
                InputStream is = conn.getInputStream();
                // Read the stream
                byte[] b = new byte[1024];
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                while (is.read(b) != -1)
                    baos.write(b);
                String JSONResp = new String(baos.toByteArray());
                System.out.println("JSONResp >>>>>>>>>>" + JSONResp);
                JSONObject obj = new JSONObject(JSONResp);

                return JSONResp;
            }

            catch (Throwable t) {
                t.printStackTrace();
                System.out.println("Throwable >>>"
                        + t.getMessage().toString());
                MyApplication.Logs(new SimpleDateFormat("yyyyMMddHHmmss").format(new Date())+getClass().toString()+"InternetAction.mapView.Throwable "+"\n"+t.getMessage().toString());

            }
            return null;
        }

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        public void onPostExecute(String result) {
            super.onPostExecute(result);
            List m = new ArrayList<String>();
            Log.w("ress", result.toString());
            StringBuilder sb = new StringBuilder(result);
            StringBuilder sb2 = sb.deleteCharAt(sb.indexOf("}"));
            System.out.println("after delete }" + sb2.toString());
            StringBuilder sb3 = sb2.deleteCharAt(sb2.indexOf("{"));
            System.out.println("after delete {" + sb3.toString());

            String[] lol = sb3.toString().split(",");
            for (int i = 0; i < lol.length; i++) {
                System.out.println("lol  ??" + lol[i]);
                System.out.println("size of lol" + lol.length);
                String lol2[] = lol[i].split(":");
                System.out.println("size of lol2" + lol2.length);
                m.add(lol2[1]);

            }
            System.out.println("lol2  ??" + m.get(0) + "lon" + m.get(1));
            StringBuilder lat_sb = new StringBuilder(m.get(0).toString());
            StringBuilder latsbb = lat_sb
                    .deleteCharAt(lat_sb.indexOf("\""));
            StringBuilder lats = latsbb.deleteCharAt(latsbb.indexOf("\""));
            System.out.println("lats " + lats);
            StringBuilder lon_sb = new StringBuilder(m.get(1).toString());
            StringBuilder lonsbb = lon_sb
                    .deleteCharAt(lon_sb.indexOf("\""));
            StringBuilder lons = lonsbb.deleteCharAt(lonsbb.indexOf("\""));
            System.out.println("lons " + lons);
            double lat = Double.parseDouble(lats.toString().trim());
            System.out.println("lat " + lat);
            double lon = Double.parseDouble(lons.toString().trim());
            System.out.println("lon " + lon);


        }
    }
    HttpGetAsyncTask httpGetAsyncTask = new HttpGetAsyncTask();
    httpGetAsyncTask.execute();
}

2 个答案:

答案 0 :(得分:0)

要致电,请使用

new HttpGetAsyncTask ().execute();

Reference


您的代码应该像

public Class OuterClass
{
    public static void mapView() { Method}
    .... Other methods....

    /*Async task called as inner class*/ 
    class HttpGetAsyncTask extends AsyncTask<String, Void, String>
    {
        @Override
         protected void onPreExecute() {
         super.onPreExecute();
         // Activities before asynctask
         } // End of onPreExecute


        @Override
        protected Void doInBackground(Void... params) {

        //Activities during asynctask (Background only)
           return null;
        }

        @Override
       protected void onPostExecute(Void result) {
       super.onPostExecute(result);
       //Activities after asynctask
       } // End of onPostExecute


    } // End of HttpGetAsyncTask  class


} // End of OuterClass

答案 1 :(得分:0)

httpAsyncTask.execute()

用于启动AsyncTask。要获得结果,您需要在工作线程完成其任务后访问它。

protected void onPostExecute(String result)
在AsyncTask类中。这个函数的参数就是结果。你可以在结果准备就绪时安全地访问它,你可以在这里做你想做的事情(调用适当的函数来处理结果,更新textview等)。这是异步方法调用背后的基本思想。你没有立即获得结果,但是当它变得可用时。