1

Here is my asynctask i want to start it from onCreate(),tried GetChildList.execute();,but it is not working,what are the parameters i need to pass based on following code.tried by passing new String[] {my url address}but not working.how to do it.what are the parameters i neeed to pass in execute method.

public class GetChildList extends AsyncTask<String, Void, String> {

    private String strm = "lat,longi";
    private String client_id = "xxx";
    private String client_secret = "xxx";
    private String currentDateandTime = "20131008"; // yyyymmdd

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        DefaultHttpClient httpclient = new DefaultHttpClient();
        final HttpParams httpParams = httpclient.getParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, 30000);
        HttpConnectionParams.setSoTimeout(httpParams, 30000);
        HttpGet httppost = new HttpGet(
                "https://api.foursquare.com/v2/venues/search?intent=checkin&ll="
                        + strm + "&client_id=" + client_id
                        + "&client_secret=" + client_secret + "&v="
                        + currentDateandTime); //

        try {

            HttpResponse response = httpclient.execute(httppost); // response
                                                                    // class
                                                                    // to
                                                                    // handle
                                                                    // responses
            jsonResult = inputStreamToString(
                    response.getEntity().getContent()).toString();

            JSONObject object = new JSONObject(jsonResult);
        } catch (ConnectTimeoutException e) {
            Toast.makeText(getApplicationContext(), "No Internet",
                    Toast.LENGTH_LONG).show();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return jsonResult;
    }

    protected void onPostExecute(String Result) {
        try {

            Toast.makeText(getApplicationContext(),
                    "R E S U L T :" + jsonResult, Toast.LENGTH_LONG).show();
            System.out.println(jsonResult);
            // showing result

        } catch (Exception E) {
            Toast.makeText(getApplicationContext(),
                    "Error:" + E.getMessage(), Toast.LENGTH_LONG).show();
        }

    }

    private StringBuilder inputStreamToString(InputStream is) {
        String rLine = "";
        StringBuilder answer = new StringBuilder();
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));

        try {
            while ((rLine = rd.readLine()) != null) {
                answer.append(rLine);
            }
        }

        catch (IOException e) {
            e.printStackTrace();
        }
        return answer;
    }

}
4

1 回答 1

2

protected String doInBackground(String... params) {'String...params' 中意味着你可以将任意数量的String 参数传递给这个函数。它不是数组或数组列表。您可以通过调用来执行您的 AsyncTask 函数

GetChildList.execute("stringr");,

GetChildList.execute("String1", "String2");,

GetChildList.execute("String1", "String2", "String2");

ETC...

于 2013-10-08T10:37:59.593 回答