I have code to log a person in with a user name password from a web server. The code works fine on my other android devices (Vergin mobile, acer tablet) which are android 2.2 and 3.1.
But it crashes on the new Galaxy phones.
when the code executes
OutputStreamWriter wr3 = new OutputStreamWriter(conn3.getOutputStream());
The code causes a exception Networkonmainthread exception and the reason is null.
I was wondering is their a security issue using android 4,1???? Since it works on my other devices which are 3.1 and 2.2?
code
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();
}
You are performing network I/O on the main thread. While allowed before Android 4.0, it was never a good idea. Please move your network I/O to a background thread, such as one supplied by an
AsyncTask.