1
  public void sendGET(String GET_URL) throws IOException {
    try {
        URL obj = new URL(GET_URL);
        HttpURLConnection con = (HttpURLConnection) obj.openConnection();
        con.setRequestMethod("GET");
        con.setRequestProperty("User-Agent", USER_AGENT);
        int responseCode = con.getResponseCode();
    }
    catch (IOException e) {
        e.printStackTrace();
    }
}

This is method that i had written for get request. GET_URL is a string in my case its value GET_URL=192.168.4.1?value=1. I had call this method on button click but it doesn't work in android while in java it work perfect. Note: my apk compiles without any error and it run also but get request doesn't work. Button code is as follow,

    led1on.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            try{
                sendGET("http://192.168.4.1/?linker=1");
                Toast.makeText(getApplicationContext(),
                        "Led1 ON Clicked!", Toast.LENGTH_SHORT).show();
            }
            catch (IOException e){

            }
        }
    });

crash log is here: 07-29 16:10:07.352 3459-3459/com.example.samiullah.esp8266 E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.samiullah.esp8266, PID: 3459 android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1273) at libcore.io.BlockGuardOs.connect(BlockGuardOs.java:110) at libcore.io.IoBridge.connectErrno(IoBridge.java:137) at libcore.io.IoBridge.connect(IoBridge.java:122) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:183) at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:452) at java.net.Socket.connect(Socket.java:884) at com.android.okhttp.internal.Platform.connectSocket(Platform.java:117) at com.android.okhttp.internal.http.SocketConnector.connectRawSocket(SocketConnector.java:160) at com.android.okhttp.internal.http.SocketConnector.connectCleartext(SocketConnector.java:67) at com.android.okhttp.Connection.connect(Connection.java:152) at com.android.okhttp.Connection.connectAndSetOwner(Connection.java:185) at com.android.okhttp.OkHttpClient$1.connectAndSetOwner(OkHttpClient.java:128) at com.android.okhttp.internal.http.HttpEngine.nextConnection(HttpEngine.java:341) at com.android.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:330) at com.android.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:248) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:433) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:384) at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(HttpURLConnectionImpl.java:497) at com.example.samiullah.esp8266.MainActivity.sendGET(MainActivity.java:110) at com.example.samiullah.esp8266.MainActivity$1.onClick(MainActivity.java:29) at android.view.View.performClick(View.java:5198) at android.view.View$PerformClick.run(View.java:21147) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 07-29 16:10:09.956 3459-3459/com.example.samiullah.esp8266 I/Process: Sending signal. PID: 3459 SIG: 9

Sami Ullah
  • 13
  • 5
  • 1
    What is the error you are getting? – Ashraff Ali Wahab Jul 29 '18 at 09:39
  • @AshraffAliWahab I am getting no Error. The get request doesn't work. This get request is recieved by arduino which in turn ON led. But when i click button it doesn't work. The same code for get request is working in java. I had called it at onClick of button in java it works and led gets ON but it doesn't works in android. All i want to do is how i can use get request in android . – Sami Ullah Jul 29 '18 at 09:46
  • Did you have android internet permission in your manifest file? – zhh Jul 29 '18 at 10:34
  • No. But i am connected to an arduino wifi i am sending this get request to arudino no internet is involved. Arduino is acting as wifi access point to which my android mobile is connected. What permissions shall i enable internet or some wifi ? – Sami Ullah Jul 29 '18 at 10:45
  • @zhh I think the problem you had identified is the cause that get request isn't working. what solution can you suggest ? – Sami Ullah Jul 29 '18 at 10:48

3 Answers3

0

In your AndroidMenifest.xml, add

<uses-permission android:name="android.permission.INTERNET" />

I see your logs, it is a common problem for those who are not familiar with android. Try

new Thread(() -> sendGET("http://192.168.4.1/?linker=1")).start();

It is not a good approach but it can solve your problem. Have a look on How do I fix android.os.NetworkOnMainThreadException?

zhh
  • 2,346
  • 1
  • 11
  • 22
-1

i think this is a funny mistake...

server on which port are listening? you should specify server port in request url like http://192.168.4.1:8080/?linker=1

hamed
  • 114
  • 2
  • 2
  • 9
  • Same code for get request in java is working without port. After making changes to port in java it work as earlier. But it had no effect in android. I want some method that can make get request as like the one above in android. When i type 192.168.4.1?linker=1 in browser it work. I want to do this task with android app for which i had made some buttton. At the back of these button i call sendGet() method. But unfortunately it doesn't work. Can you suggest some method that could be called on button click and it will send get request 192.168.4.1?linker=1 ? – Sami Ullah Jul 29 '18 at 10:39
-1

You have to connect with same network first then try to fetch the data. Or try some other API link. eg. https://jsonplaceholder.typicode.com/photos/1