从main方法调用AsyncTask方法

时间:2013-10-29 17:04:19

标签: android json android-asynctask google-direction

伙计们,我创建了一个应用程序,它向Google方向Web服务发出HTTPRequest,然后返回JSON对象。

这是我的JSONParser.java看起来像

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

这是我的responseListener.java

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

我想调用方法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();
        }
    }
});

但它一直说Type mismatch: cannot convert from void to String 关于如何解决这个问题的任何想法?提前谢谢你

2 个答案:

答案 0 :(得分:1)

这是因为您的return typevoid

public void getJSONFromUrl

将退货类型更改为Stringreturn String

在这一行

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

但该方法明确返回void

正如迭戈苏亚雷斯在评论中指出的那样,不需要这个response变量......这就是回调的用途。您可以将其更改为

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

onPostExecute()完成时,将启动回调。

答案 1 :(得分:0)

因为你的方法

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

的返回类型为 void

并在下面的行

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

您正尝试在字符串响应中设置此内容。

您需要更改

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

并从此方法返回响应字符串。