简体   繁体   中英

How to make multiple okhttp3 requests at the same time Android Studio

Good Afternoon, I am still very new to ESP32/android studio coding so I apologize for my beginner terminology. I am currently coding a project where I can control multiple stepper motors at the same exact time from the press of a button on my android application and the motors are connected to certain ESP32 GPIO pins, I am using the okhttp3 client as well. My code is below.

public class Connectivity {
public static String geturl (String url_esp32){
    OkHttpClient client = new OkHttpClient();
    Request request = new Request.Builder()
            .url(url_esp32)
            .build();


    try
    {
        Response response = client.newCall(request).execute();
        return response.body().string();

    } catch(IOException error) {

        return error.toString();

    }

}

}

above is my connectivity page for connecting to the requests for the esp32.

  PBNow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // request information from esp32
            // PB sandwich now, disable jelly motor
            request_to_url("STEP");
            request_to_url("DIR");
            request_to_url("STEP2");
            request_to_url("DIR2");
            request_to_url("STEP4");
            request_to_url("DIR4");
            request_to_url("ledRED");
            request_to_url("ledGREEN");
        }
    });

above is how im calling the requests for the esp32. The problem I am having is that when these request_to_url lines are going line by line but I want them to all run at the exact same time. Is this possible.

Below are also my request_to_url function and request_data function.

public void request_to_url (String command) {
    ConnectivityManager connMgr = (ConnectivityManager)
            getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();

    if(networkInfo != null && networkInfo.isConnected()) {

        new request_data().execute("http://" + ip_address + "/" + command);

    }else {
        Toast.makeText(activity_2.this, "Not connected  ", Toast.LENGTH_LONG).show();

    }
}


private class request_data extends AsyncTask<String, Void, String> {

    @Override
    protected String doInBackground(String... url)
    {
        return Connectivity.geturl(url[0]);
    }

    @Override
    protected void onPostExecute(String result_data) {
        if(result_data != null)
        {

        }else{
            Toast.makeText(activity_2.this, "Null data", Toast.LENGTH_LONG).show();
        }
    }
}

I apologize if the code is very sloppy, I am still very new. Thank you very much.

Besides the messy code in your app required to fire off multiple requests, the ESP32 has a very limited network stack and resources, and cannot handle many simultaneous connections. If your app opens too many HTTP connections to the ESP32 at once, some will likely fail or have to wait for others to close.

Instead, you can do it all in a single request and tell the ESP32 to do multiple things at once. Pass the requests as parameters in the URL, like so:

http://ip-address/cmd?step=1&dir=0&ledGREEN=0&ledRED=1

Just have the handler on the ESP32 for the path /cmd look for the presence of each possible parameter and respond to it appropriately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM