hello先生在登录页面我正在验证来自服务器数据库的用户名和密码。如果有网络意味着我的申请工作正常。如果我断开互联网意味着我的应用运行意味着它显示错误应用程序没有响应。如何消除这种类型的错误
i try this code
if(name.equals("") || pass.equals(""))
{
Toast.makeText(Main.this, "Please Enter Username and Password", Toast.LENGTH_LONG).show();
}
else
{
try {
httpclient = new DefaultHttpClient();
httppost = new HttpPost("server url/login.php");
// Add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("UserEmail", name.trim()));
nameValuePairs.add(new BasicNameValuePair("Password", pass.trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
// Execute HTTP Post Request
response = httpclient.execute(httppost);
inputStream = response.getEntity().getContent();
data = new byte[256];
buffer = new StringBuffer();
int len = 0;
while (-1 != (len = inputStream.read(data)) )
{
buffer.append(new String(data, 0, len));
}
inputStream.close();
}
catch (IOException e) {
System.out.println(e);
//alertDialog.cancel();
}
if(buffer.charAt(0)=='Y')
{
Intent intent=new Intent(getApplicationContext(),ManagerHandset.class);
startActivity(intent);
}
else
{
Toast.makeText(Main.this, "Invalid Username or password", Toast.LENGTH_LONG).show();
}
}
}
});
如果我想断开我的网意味着如何显示警报网不可用
答案 0 :(得分:1)
您可以通过以下类型的功能检查您的互联网连接:
public boolean isNetworkAvailable(Context context) {
boolean value = false;
ConnectivityManager connec = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connec.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED
|| connec.getNetworkInfo(1).getState() == NetworkInfo.State.CONNECTED) {
value = true;
}
// Log.d ("1", Boolean.toString(value) );
return value;
}
请记住,您在清单文件中添加了以下权限:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
修改强>
if (isNetworkAvailable(getApplicationContext()))
{
// Do whatever you want to do
}
else{
new AlertDialog.Builder(YourActivitName.this)
.setTitle("Error")
.setMessage("Your Internet Connection is not available at the moment. Please try again later.")
.setPositiveButton(android.R.string.ok, null)
.show();
}
希望这对你有用......
答案 1 :(得分:0)
您可以使用以下内容:
private boolean isNetworkAvailable() {
ConnectivityManager connectivityManager = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo activeNetworkInfo = connectivityManager
.getActiveNetworkInfo();
return activeNetworkInfo != null;
}
如果它返回true
继续您的正常代码,则显示消息..也在您的清单文件中添加
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
答案 2 :(得分:0)
如果网络可用,请检查互联网连接..
private boolean isOnline()
{
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnected())
{
return true;
}
else
{
return false;
}
}
答案 3 :(得分:0)
在说出网络电话之前检查网络状态是一个好习惯,其他答复中解释的所有方法都是正确的。链接到android doc。
如果您的应用程序在网络断开连接时没有响应,则表示您在主线程上声明了您的HTTP请求。你永远不应该这样做。
即使网络通过Wifi连接正常工作,在慢速EDGE连接上也会遇到相同的“无响应”错误。
要在主线程之外发出HTTP请求,您应该使用AsyncTask,原则在android doc中解释。