user2843350
user2843350

Reputation:

what are the parameters used to start asynctask in android

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

}

Upvotes: 1

Views: 512

Answers (1)

Khawar Raza
Khawar Raza

Reputation: 16120

In protected String doInBackground(String... params) { the 'String... params' means that you can pass any number of String parameters to this function. It is not an array or arraylist. You can execute your AsyncTask function by calling like

GetChildList.execute("stringr");,

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

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

etc...

Upvotes: 2

Related Questions