0

i have create an android application on where the user can select the start and end point of the location. This application will use the Google-Direction web service and make the HTTPRequest. I will make this as short, I want to call the asynctask method in the JSONParser class from the main_activity. The issue is, I don't know how to display the result in the main_activtiy method

here is the asynctask method

public class JSONParser {
InputStream is = null;
JSONObject jObj = null;
String json = "";

public JSONParser() {
}

public void getJSONFromUrl(final String url, final responseListener target) {
    new AsyncTask<Void, Void, String>() {
        protected String doInBackground(Void... params) {
            HttpURLConnection httpURLConnection = null;
            StringBuilder stringBuilder = new StringBuilder();
            try {
                httpURLConnection = (HttpURLConnection) new URL(url).openConnection();
                InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());

                int read;
                char[] buff = new char[1024];
                while ((read = inputStreamReader.read(buff)) != -1) {
                    stringBuilder.append(buff, 0, read);
                }
                return stringBuilder.toString();
            } catch (MalformedURLException localMalformedURLException) {
                return "";
            } catch (IOException localIOException) {
                return "";
            } finally {
                if (httpURLConnection != null)
                    httpURLConnection.disconnect();
            }

        }

        protected void onPostExecute(String result) {
            super.onPostExecute(result);
            target.onResponseComplete(result);
        }
    }.execute();
}

here is how the main method is calling the method

new JSONParser().getJSONFromUrl(url, new responseListener() {

        @Override
        public void onResponseComplete(String response) {
            try {
                ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
                JSONArray step = new JSONObject(response).getJSONArray("routes").getJSONObject(0).getJSONArray("legs")
                        .getJSONObject(0).getJSONArray("steps");

                for (int i = 0; i < step.length(); i++) {
                    HashMap<String,Object> row = new HashMap<String,Object>();
                    row.put("Distance", step.getJSONObject(i).getJSONObject("distance").getString("text"));
                    list.add(row);
                }

            }catch (Exception e){
                e.printStackTrace();
            }
        }
    });
}

the issue right know is how i want to display the Arraylist List value and put it into the TextView call jarak

MAJ
  • 27
  • 1
  • 6
  • http://stackoverflow.com/questions/16752073/how-do-i-return-a-boolean-from-asynctask. use interface – Raghunandan Oct 30 '13 at 14:31
  • 1
    @Raghunandan I believe the OP is using an `interface` with `public void onResponseComplete(String response)`. And there is a `callback` in `onPostExecute()` with `target.onResponseComplete(result);`. So the question becomes, is that working properly and is the `ArrayList` being populated correctly? – codeMagic Oct 30 '13 at 15:07
  • dear @codeMagic , is there any modification that i need to do ? really need ur help – MAJ Oct 30 '13 at 15:32
  • I'm waiting for you to answer the question. Are you getting the correct values in `list` and don't know how to display them? Or are you not getting the values? – codeMagic Oct 30 '13 at 15:34
  • I don't know whether i get the correct value. Because i don't know to extract the value inside the 'list'. thank you in advance @codeMagic – MAJ Oct 30 '13 at 15:46
  • Put a breakpoint in the `for loop` and see if `list` is being populated. – codeMagic Oct 30 '13 at 15:54
  • what is the meaning of breakpoint ? I'm sorry @codeMagic , I'm just amateur. – MAJ Oct 30 '13 at 15:58
  • [See this answer](http://stackoverflow.com/questions/4527159/eclipse-java-breakpoints-what-is-the-purpose) – codeMagic Oct 30 '13 at 16:01
  • @codeMagic yes your right i was too lazy to go through the full post. – Raghunandan Oct 30 '13 at 18:23
  • @codeMagic , I have put the breakpoint as u told me to. The list is being populate correctly. I want to display the value outside the calling method `new JSONParser().getJSONFromUrl(url, new responseListener() ...` Can you show me how ? – MAJ Oct 31 '13 at 06:45

1 Answers1

0

You can change your List to be

ArrayList<HashMap<String, String>>

as you are getting a string from

step.getJSONObject(i).getJSONObject("distance").getString("text")

To get it out you can use (assuming your textview is called jarak)

for(HashMap<String,String> map : list) {
     for(Entry<String, String> entry : map.entrySet()) {
         jarak.setText(entry.getKey() + ", " + entry.getValue());
     }
}

Hope that helps

MungoRae
  • 1,912
  • 1
  • 16
  • 25
  • One more thing @MungoRae , the code that you gave to me is running smoothly if I put it inside the `new JSONParser().getJSONFromUrl(url, new responseListener() ...` but when I it outside, it show me error. For your information, I want to display it outside the method. Thank you. – MAJ Nov 01 '13 at 11:49