skip to Main Content

I am working on esp8266 and wanted to make application on Android studio.
My Problem is the when i press push button on the application ,the url open which i dont want do it.Because if the url open there is no make sense to see the url.

Other people also answered it (Execute URL without opening browser in Android ,Starting an ACTION_VIEW activity to open the browser, how do I return to my app?)
but,it not working for me.
Kindly Help me !

Android studio java code

import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;

public class MainActivity extends AppCompatActivity {


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    
    public void clicking(View view) throws IOException {
    //        Intent intent = new Intent(Intent.ACTION_VIEW, 
        Uri.parse("http://192.168.4.1/LEDON?"));
    
    //        startActivity(intent);
    
        URL myURL = new URL("http://192.168.4.1/LEDON?");
        URLConnection myURLConnection = myURL.openConnection();
    
        myURLConnection.getContent(); // -> calls getHeaderField("content-lengt
    //        URL myURL = new URL("http://192.168.4.1/LEDON?");
    //        URLConnection myURLConnection = myURL.openConnection();
    
    //        try (InputStream is = myURLConnection.getInputStream()) {}
    }
}

2

Answers


  1. activity_main.xml

    <WebView
            android:id="@+id/webView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    

    MainActivity

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    
        mWebView = (WebView) findViewById(R.id.webView);
        WebSettings webSettings = mWebView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        mWebView.loadUrl("http://192.168.4.1/LEDON?");
    }
    
    Login or Signup to reply.
  2. You can add a webview widget in xml file of activity then you have to connect it with activity.java file and then have to add the INTERNET user permission in the manifest file, but one last thing for opening http server is allowing your app
    Uses clear text traffic.(to open http website).
    Actually webview by default don’t open unsafe website, without this step you can still open the https website as it is secure and uses encryption. On the other side http is plain text. so for opening this file you have to follow the last step other wise it webpage will not be loaded by saying – ERR_CLEARTEXT_NOT_PERMITTED

    All the files

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search