Question

hye guys, I create an application where it make an HTTPRequest to the google direction web service and it return the JSONobject.

This is my JSONParser.java look like

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();
}

and this is my responseListener.java

interface responseListener{
public void onResponseComplete(String response); }

I want to call the method getJSONFromURL

String response = new JSONParser().getJSONFromUrl(makeURL(startLatitude,startLongitude,endLatitude,endtLongitude),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("address", step.getJSONObject(i).getString("html_instructions"));
                row.put("start",new LatLng(Double.parseDouble(step.getJSONObject(i).getJSONObject("start_location").getString("lat")), Double.parseDouble(step.getJSONObject(i).getJSONObject("start_location").getString("lng"))));
                row.put("end",  new LatLng(Double.parseDouble(step.getJSONObject(i).getJSONObject("start_location").getString("lat")), Double.parseDouble(step.getJSONObject(i).getJSONObject("start_location").getString("lng"))));
                list.add(row);

            }

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

but it keep saying that Type mismatch: cannot convert from void to String any idea on how to solve this ? thank you in advance

Was it helpful?

Solution

This is because your return type is void

public void getJSONFromUrl

Change the return type to String and return a String.

In this line

String response = new JSONParser().getJSONFromUrl(makeURL(startLatitude,startLongitude,endLatitude,endtLongitude),new responseListener()

but the method clearly returns void.

As Diego Suárez pointed out in a comment, there is no need for this response variable...that is what the callback is for. You can change that to

new JSONParser().getJSONFromUrl(makeURL(startLatitude,startLongitude,endLatitude,endtLongitude),new responseListener()

And the callback will be initiated when onPostExecute() finishes.

OTHER TIPS

Because your method

 public void getJSONFromUrl(final String url, final responseListener target) 

has a return type as void.

and by below line

String response = new JSONParser().getJSONFromUrl(makeURL(startLatitude,startLongitude,endLatitude,endtLongitude),new responseListener() {

You are trying to set the this in your String response.

You need to change

 public String getJSONFromUrl(final String url, final responseListener target) 

and return a response String from this method.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top