Y
Y
yaroclav-Nii2021-01-13 18:04:35
Java
yaroclav-Nii, 2021-01-13 18:04:35

How to send an argument to a class?

I just want to send the location to the MyAsyncTask class, but I don’t understand how anyone can tell me, here’s the code

private String formatLocation(Location location) {
        if (location == null)
            return "";
        new MyAsyncTask(location).execute();

        return String.format(
                "Coordinates: lat = %1$.4f, lon = %2$.4f, time = %3$tF %3$tT",
                location.getLatitude(), location.getLongitude(), new Date(
                        location.getTime()));



    }
    class MyAsyncTask() extends AsyncTask<String, String, String> {

        
        String server = "http://demo.harrix.org/demo0011";
        String a, b, answerHTTP;
        a = ((Double) location.getLatitude()).toString();
        b = ((Double) location.getLatitude()).toString();

        @Override
        protected String doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();

            HttpGet httpget = new HttpGet(server + "?a=" + a + "&b=" + b);

            try {
                HttpResponse response = httpclient.execute(httpget);

                if (response.getStatusLine().getStatusCode() == 200) {
                    HttpEntity entity = response.getEntity();
                    answerHTTP = EntityUtils.toString(entity);
                }
            }
            catch (ClientProtocolException e) {
            }
            catch (IOException e) {
            }

            return null;
        }}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
acwartz, 2021-01-14
@acwartz

params is a list of parameters.
run like this:
new MyAsyncTask().execute(location);
but in general it should be like this:
new MyAsyncTask().execute(location.lat, location.long, location.time);
then you can get it from params and substitute it in the request:

doInBackground(String... params) {
            HttpClient httpclient = new DefaultHttpClient();
                                                              lat                             long              time
            HttpGet httpget = new HttpGet(server + "?a=" + params[0] + "&b=" + params[1] +...params[3]);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question