0

我有代码可以使用来自 Web 服务器的用户名密码登录一个人。该代码在我的其他 android 设备(Vergin 手机、acer 平板电脑)上运行良好,它们是 android 2.2 和 3.1。但它在新的 Galaxy 手机上崩溃了。

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

该代码导致异常 Networkonmainthread 异常,原因为空。我想知道他们使用 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();
}
4

1 回答 1

1

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

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

于 2013-01-18T16:53:55.287 回答