Android 主线程上的操作系统网络异常

Android 主线程上的操作系统网络异常,android,mysql,Android,Mysql,这将获取操作系统网络在主线程异常上的错误。。我是怎么决定的!?? 我正在使用localhost服务器 在允许的情况下,我激活了它,但仍然不工作 public void GetText() throws UnsupportedEncodingException{ String email = mEmailView.getText().toString(); String password = mPasswordView.getText().toString(); //u

这将获取操作系统网络在主线程异常上的错误。。我是怎么决定的!?? 我正在使用localhost服务器 在允许的情况下,我激活了它,但仍然不工作

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  );

}

在这个区块里运行你的代码我试过了

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

thread.start(); 

在发布问题之前仔细搜索。网络操作可以是长时间运行的操作,而不是在主线程上执行。使用异步任务或单独的非UI线程执行长时间运行的操作。更改URL localhost不能在URL中使用,请使用URL ip地址