0

I am new to Android & Java and i want to access and get value from void Asynctask like this:

Here is the whole code of Asynctask, I want to pass values of lan & lon from onPostExecuted to another activity Edited

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();
}
nnank12
  • 35
  • 5

2 Answers2

0

To call, use

new HttpGetAsyncTask ().execute();

Reference


Your code should be like

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
Community
  • 1
  • 1
ngrashia
  • 9,869
  • 5
  • 43
  • 58
0
httpAsyncTask.execute()

is used to start AsyncTask. To get the result, you need to access it where it becomes available, once the worker thread has completed its task.

protected void onPostExecute(String result)

in your AsyncTask class. The parameter to this function is the result.Here you can safely access it when the result is ready and you can do with it here what you want (call an appropriate function to handle the result, update a textview etc). This is the generel idea behind asynchronous method calls. You don't have the result right away, but when it becomes available.

jaimin
  • 563
  • 8
  • 25
  • yes i know.. i want to pass values from it to another activity – nnank12 Aug 05 '14 at 09:57
  • Right now i cant give you code i am out of my office. But i can help you. 1. Create an intent, 2 use putExtra() to pass variables 3. Start Activity. 3 in your activity use getExtra() to get variable values – jaimin Aug 05 '14 at 10:33