-1

this gots the error of os network on main thread exception.. how i resolve!?? i am using localhost server in permission i active that and still not working

public void GetText() throws UnsupportedEncodingException{

    String email = mEmailView.getText().toString();
    String password = mPasswordView.getText().toString();

    //urlTest = "http://localhost/php/projectsirius-the-rgstr/backoffice/login.php&id="+email;

    String data = URLEncoder.encode("email", "UTF-8")
            + "=" + URLEncoder.encode(email, "UTF-8");

    data += "&" + URLEncoder.encode("pass", "UTF-8")
            + "=" + URLEncoder.encode(password, "UTF-8");
    String text = "";
    BufferedReader reader=null;

    try
    {
        URL url = new URL("http://localhost/php/projectsirius-the-rgstr/backoffice/test.php");
        URLConnection conn = url.openConnection();
        conn.setDoOutput(true);
        OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
        wr.write( data );
        wr.flush();

        // Get the server response

        reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        StringBuilder sb = new StringBuilder();
        String line = null;

        // Read Server Response
        while((line = reader.readLine()) != null)
        {
            // Append server response in string

            sb.append(line + "\n");
        }


        text = sb.toString();

    }
    catch(Exception e)
    {
        Log.e("error","ERROR" + e.toString());
    }
    finally
    {
        try
        {

            reader.close();
        }

        catch(Exception ex) {}
    }

    // Show response on activity
    content.setText( text  );

}
Rince Thomas
  • 4,158
  • 5
  • 25
  • 44
  • search well before post the question. http://stackoverflow.com/questions/6343166/android-os-networkonmainthreadexception – Remees M Syde Mar 18 '15 at 11:29
  • Network operations can be long running operations, you dont do them on the main thread. Use an Async Task or a separate non UI thread to do long running operations. – Skynet Mar 18 '15 at 11:30

1 Answers1

0

Run your code in this block i tried it

 Thread thread = new Thread(new Runnable(){
@Override
public void run() {
    try {
        //Your code goes here
    } catch (Exception e) {
        e.printStackTrace();
    }
}
});

thread.start(); 
siddhesh
  • 563
  • 1
  • 9
  • 15