Web サーバーからユーザー名パスワードを使用してログインするコードがあります。コードは、Android 2.2 および 3.1 である他の Android デバイス (Vergin モバイル、acer タブレット) で正常に動作します。しかし、新しい 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();
}