Search code examples
javahttpurlconnection

How to execute http cmmand directly with Android Java?


I have a custom device which it's connected via hotspot with my mobile and through my app I can connect and login to that custom device correctly like :

http://x.x.x.x/cgi-bin/web.cgi?action=API&action2=Login&param1=username&param2=password

using this connection sequence:

url = new URL(URLCommand);
connection = (HttpURLConnection) url.openConnection();

if (android.os.Build.VERSION.SDK_INT > 9)
{
    StrictMode.ThreadPolicy policy;
    policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}

int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK || responseCode == HttpURLConnection.HTTP_CREATED) {

and I can obtain correctly responseCode = 200 which is OK. Then I try to send another HTTP url to try to restart that custom device like:

http://x.x.x.x/cgi-bin/web.cgi?session_id=xxxxxxxxxx&action=API&action2=value1&param1=RESTART

using as POST:

url = new URL(URLCommand);
connectionForCommand = (HttpURLConnection) url.openConnection();
connectionForCommand.setRequestMethod("POST");
connectionForCommand.setDoOutput(true);
BufferedReader responseReader = new BufferedReader(new InputStreamReader(connectionForCommand.getInputStream()));
String responseData = responseReader.readLine();

so I thought that setDoOutput(true) would "execute" that url and make that custom device to reboot...

I tried also to put this code above inside thread using lambda as like as :

Thread = () -> {}

to avoid IOException about running it in DialogFragment's main activity but it didn't worked. Is there maybe a way to execute that http without using any third parts? Or do I have to use any only third parts to reach that goal because HttpURLConnection doesn't fit for that? Thanks! Cheers

EDIT:

I tried also using Volley library doing a StringRequest like :

new Thread(() -> {

    urlPostRequest = "http://x.x.x.x/cgi-bin/web.cgi?session_id=xxxxxxxxxx&action=API&action2=value1&param1=RESTART";
    try{
        RequestQueue arq = Volley.newRequestQueue(getContext());
        arq.getCache().clear();

        StringRequest urlPostRequest = new StringRequest(Request.Method.POST, URLCommand,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    try {
                        JSONObject obj = new JSONObject(response);
                        String  value= obj.getString("value");
                        showAlert("LogCat", value.toString());
                    } catch(JSONException e) { e.printStackTrace(); 
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    showAlert("LogCat", getActivity().getResources().getString(R.string.st_errorresponse));
                }
        });
        arq.add(urlPostRequest);
    } catch (Exception e) { e.printStackTrace(); }

}).start();

but neither this way didn't worked even I settled as POST....maybe I missed something to consider?...


Solution

  • Have you tried OkHttp library?

    Here is one Android POST example with OkHttp..

    https://www.digitalocean.com/community/tutorials/okhttp-android-example-tutorial