I wrote a landing click event, access the server at login time, there is a network status between the judge.
If I open the application, enter the login screen when the phone network, then no problem
successful landingThe problem is that if I get into the login screen when the phone is not network login fails with "You currently do not have a network connection", but this time connected to the Internet, (Note: At this point my phone networking, remained at the login screen), click on the landing is still landing failure, I suggest there is no network connection.
The following is my code
public class LoginActivity extends Activity {
private ProgressDialog pd;
private boolean isNetWork;
private EditText username;
private EditText password;
private String nameStr;
private String passwordStr;
private SharedPreferences shared;
private Intent service;
@ Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_login);
username = (EditText) this.findViewById (R.id.username);
password = (EditText) this.findViewById (R.id.password);
username.setText ("admin");
password.setText ("123456");
pd = new ProgressDialog (this);
pd.setMessage ("Loading in ......");
}
/ **
* Click the Login button to trigger an event
* @ Throws Exception
* /
public void btn_login (View v) throws Exception {
nameStr = username.getText () toString () trim ();..
passwordStr = password.getText () toString () trim ();..
if (StringUtils.isEmpty (nameStr) | | StringUtils.isEmpty (passwordStr)) {
Toast.makeText (LoginActivity.this, "I'm sorry, the username and password can not be empty!!", 5) show ();.
} Else {
new LoginTask () execute ();.
}
}
/ **
* Asynchronous tasks
landing incident* @ Author scuser
*
* /
public final class LoginTask extends AsyncTask> {
@ Override
protected void onPreExecute () {
pd.show ();
}
@ Override
protected MapdoInBackground (String. .. params) {
try {
String path = Constant.URL.login + "/" + nameStr + "/" + passwordStr;
Mapmap = new HashMap ();
map.put ("name", nameStr);
map.put ("password", passwordStr);
byte [] data = Tools.sendHttpGet (path, map);
String json = new String (data, "utf-8");
return parseJson (json);
} Catch (Exception e) {
e.printStackTrace ();
}
return null;
}
@ Override
protected void onPostExecute (Mapresult) {
pd.dismiss ();
if (! isNetWork) {
Toast.makeText (LoginActivity.this, "You currently do not have a network link, check the network settings!!", Toast.LENGTH_LONG) show ();.
return;
} if (result == null) {
Toast.makeText (LoginActivity.this, "the network link is abnormal!!", Toast.LENGTH_LONG) show ();.
return;
} Else {
String content = (String) result.get ("code");
if (isNetWork == true && "SUCCESS". equals (content)) {
shared = LoginActivity.this.getSharedPreferences ("userInfo", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = shared.edit ();
String name = (String) result.get ("name");
String password = (String) result.get ("password");
editor.putString ("name", name);
editor.putString ("password", password);
editor.commit ();
Intent intent = new Intent (LoginActivity.this, MainActivity.class);
startActivity (intent);
} Else if (isNetWork == true && "FAIL_BUS". Equals (content)) {
Toast.makeText (LoginActivity.this, "I'm sorry your user name or password is incorrect!!!", 5) show ();.
}
}
}
}
/ **
Broadcast
* to monitor network status* /
public BroadcastReceiver receiver = new BroadcastReceiver () {
@ Override
public void onReceive (Context context, Intent intent) {
/ / Get the network connection status
boolean netState = intent.getBooleanExtra ("netState", true);
isNetWork = netState;
}
};
@ Override
protected void onDestroy () {
super.onDestroy ();
unregisterReceiver (receiver);
stopService (service);
}
@ Override
protected void onStart () {
super.onStart ();
/ / In this starts a service to monitor network status
service = new Intent ();
service.setAction ("com.tematch.networkstate");
startService (service);
/ / Dynamically register a broadcast
IntentFilter filter = new IntentFilter ();
filter.addAction ("com.tematch.recevier");
registerReceiver (receiver, filter);
}
}
This is the code
service
public class NetworkStateService extends Service {
@ Override
public IBinder onBind (Intent intent) {
return null;
}
@ Override
public void onCreate () {
super.onCreate ();
}
@ Override
public int onStartCommand (Intent intent, int flags, int startId) {
boolean isconnect = false; / / used to determine whether the network connection
/ / Get a network connection service
ConnectivityManager manager = (ConnectivityManager)
this.getSystemService (Context.CONNECTIVITY_SERVICE);
/ / Get the connection status of WIFI
. State state = manager.getNetworkInfo (ConnectivityManager.TYPE_WIFI) getState ();
if (State.CONNECTED == state) {/ / proved successful WIFI connection, WIFI network
being usedisconnect = true;
Toast.makeText (this, "you're using WIFI connection", 5) show ();.
}
state = manager.getNetworkInfo (ConnectivityManager.TYPE_MOBILE) getState ();.
if (State.CONNECTED == state) {
isconnect = true;
Toast.makeText (this, "you're using GPRS network connection", 5) show ();.
}
if (intent! = null) {
intent.putExtra ("netState", isconnect);
intent.setAction ("com.tematch.recevier");
sendBroadcast (intent);
}
if (! isconnect) {
Toast.makeText (this, "your network connection interrupted", Toast.LENGTH_LONG) show ();.
}
return super.onStartCommand (intent, flags, startId);
}
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
I do not know yet clear description of the problem? Seek expert advice, how to change this bug fixes

Reply:
http://www.jb51.net/article/32370.htm
Reply:
Zaiding seek Reply
Reply:
You breakpoint debugging, look after the re-connection / / get network connection status
boolean netState = intent.getBooleanExtra ("netState", true);
isNetWork = netState;
Is TRUE
Reply:
I feel the problem is I click on the event landing approach seems to determine the network status only once
Reply:
so easy

Reply:
You are to soy sauce it?
Reply:
The reason you should change the value of the network after landing failed state isconnect does not change it,
Reply:
I solved the problem turned out to be less in the landing click method to determine which added network status
Reply:
Thanks for your replies
Reply:
You called problem is not clear, so much code stickers, meaningful! The problem must be dapper
Reply:
Well educated, thank you
No comments:
Post a Comment