-3

I'm trying to do a POST request in an android studio like this.

 btnSignin.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {


            //submitForm();
            tryLogin("test@test.com", "Test1234");
        }
    });





protected void tryLogin(String mUsername, String mPassword)
 {
    HttpURLConnection connection;
    OutputStreamWriter request = null;

    URL url = null;
    String response = null;
    String parameters = "email="+mUsername+"&password="+mPassword;
    StrictMode.ThreadPolicy policy = new 
    StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);

    try
    {
        url = new URL("URL");
        connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        connection.setRequestMethod("POST");

        request = new OutputStreamWriter(connection.getOutputStream());
        request.write(parameters);
        request.flush();
        request.close();
        String line = "";
        InputStreamReader isr = new InputStreamReader(connection.getInputStream());
        BufferedReader reader = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        while ((line = reader.readLine()) != null)
        {
            sb.append(line + "\n");
        }
        // Response from server after login process will be stored in response variable.
        response = sb.toString();
        // You can perform UI operations here
        Toast.makeText(this,"Message from Server: \n"+ response, 0).show();
        isr.close();
        reader.close();

    }
    catch(IOException e)
    {
        // Error
    }

But the app crashes and I get an error like this -

FATAL EXCEPTION: main

Process: test, PID: 19928

android.os.NetworkOnMainThreadException at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:>1425)

at java.net.Inet6AddressImpl.lookupHostByName(Inet6AddressImpl.java:102)

I have added internet permission in the manifest file. Is this because I have not done it via AsyncTask? How can I fix this?

Harshal Deshmukh
  • 1,787
  • 3
  • 14
  • 25
Nee
  • 71
  • 3
  • 10
  • use AsyncTask ..... – Omar Dhanish Aug 28 '17 at 05:28
  • 1
    You can't perform network operations on main thread.. so you have to use asynctask – AbhayBohra Aug 28 '17 at 05:29
  • 1
    use separate thread for network operation. asynctask is easy in this case – AAA Aug 28 '17 at 05:29
  • it's recomended to use networking library such us [okhttp](https://github.com/square/okhttp), [retrofit](http://square.github.io/retrofit) or [volley](https://github.com/google/volley). It's easier to do network operation with networking library – zihadrizkyef Aug 28 '17 at 05:30
  • StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); StrictMode.setThreadPolicy(policy); **Add this 2 line before Try Block – Harshal Deshmukh Aug 28 '17 at 05:39

1 Answers1

-1

you can put your network operation in this thread or use asynctask or any other library

new Thread(new Runnable() { 
            public void run(){        
            txt1.setText("Thread!!");
            }
        }).start();
AAA
  • 485
  • 6
  • 23