Android Galaxy OutputStreamWriter导致了一个例外,但在我所有其他Android设备上运行

时间:2013-01-18 16:45:16

标签: android

我有代码使用来自Web服务器的用户名密码来记录某个人。该代码在我的其他Android设备(Vergin mobile,acer tablet)上工作正常,它们是android 2.2和3.1。 但它在新款Galaxy手机上崩溃了。

代码执行时  OutputStreamWriter wr3 = new OutputStreamWriter(conn3.getOutputStream());

代码导致异常Networkonmainthread异常,原因为null。 我想知道他们使用android 4,1的安全问题????因为它适用于3.1和2.2的其他设备?

try {
    // Construct data
    Editable ed = mPassWord.getText();
    cGlobals.PassWord=ed.toString();

    ed = mUserName.getText();
    cGlobals.UserName=ed.toString();

    String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode( cGlobals.UserName, "UTF-8");
    data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(  cGlobals.PassWord, "UTF-8");


    // Send data
    URL url = new URL("http://www.tellafortune.com/mobile/login.php");
    // URL url = new URL("http://www.hhhhtellafortune.com/mobile/login.php");

    URLConnection conn = url.openConnection();
    conn.setDoOutput(true);
    OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());

    wr.write(data);
    wr.flush();

    // Get the response

    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    String line;
    while ((line = rd.readLine()) != null) {
        out += line;
    }

    wr.close();
    rd.close();

    if (!out.contains("ok"))
        return false;    

    // Send data
    URL url2 = new URL("http://www.tellafortune.com/mobile/getaddr.php");
    // URL url = new URL("http://www.hhhhtellafortune.com/mobile/login.php");

    URLConnection conn2 = url2.openConnection();
    conn2.setDoOutput(true);
    OutputStreamWriter wr2 = new OutputStreamWriter(conn2.getOutputStream());

    wr2.write(data);
    wr2.flush();

    // Get the response

    BufferedReader rd2 = new BufferedReader(new InputStreamReader(conn2.getInputStream()));
    String line2;
    out="";
    while ((line2 = rd2.readLine()) != null) {
        out+=line2;
    }

    wr2.close();
    rd2.close();

    cGlobals.strServerAddre=out.replaceAll("\n","");



    ////////////////////////////////////////////////////////////////////
    //////// Get port number if it is the scocial game
    if (ChatOrGame != 1) {
        // String port=new String(":4999/add");
        String port=new String(":4998/add");
        URL url3 = new URL( cGlobals.strServerAddre + port);

        URLConnection conn3 = url3.openConnection();
        conn3.setDoOutput(true);
        // crashes on this line
        OutputStreamWriter wr3 = new OutputStreamWriter(conn3.getOutputStream());

        wr3.write(data);
        wr3.flush();

        // Get the response

        BufferedReader rd3 = new BufferedReader(new InputStreamReader(conn3.getInputStream()));
        String line3;
        out="";
        while ((line3 = rd3.readLine()) != null) {
            out+=line3;
        }

        wr3.close();
        rd3.close();

        cGlobals.strPortNumber=out.replaceAll("PORT:","");
    }
} catch (Exception e) {
    new AlertDialog.Builder(this)
        .setTitle("Internet Error")
        .setMessage( "Cannot talk to server" )
        .setNeutralButton("close",new DialogInterface.OnClickListener();
}

1 个答案:

答案 0 :(得分:1)

  

代码导致异常Networkonmainthread异常,原因为null。

您正在主线程上执行网络I / O.虽然在Android 4.0之前允许,但这绝不是一个好主意。请将您的网络I / O移动到后台线程,例如AsyncTask提供的后台线程。