在android中如何避免网络断开时应用程序不响应
我创建登录页面。此登录页从服务器数据库进行验证。如果网络可用,则表示单击“登录”按钮表示工作正常。如果我禁用网络,然后单击“登录”按钮,则表示应用程序未响应时出现错误。。如何使用我的代码避免此错误在android中如何避免网络断开时应用程序不响应,android,Android,我创建登录页面。此登录页从服务器数据库进行验证。如果网络可用,则表示单击“登录”按钮表示工作正常。如果我禁用网络,然后单击“登录”按钮,则表示应用程序未响应时出现错误。。如何使用我的代码避免此错误 i try this code login.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { na
i try this code
login.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
name = username.getText().toString();
pass = password.getText().toString();
String Str_check2 = app_preferences.getString("checked", "no");
if(Str_check2.equals("yes"))
{
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("username", name);
editor.putString("password", pass);
editor.commit();
}
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("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(),another.class);
startActivity(intent);
}
else
{
System.out.println("Invalid username and password")
}
}
});
我尝试一下这个代码
login.setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图v)
{
name=username.getText().toString();
pass=password.getText().toString();
String Str_check2=app_preferences.getString(“checked”,“no”);
如果(Str_check2.equals(“yes”))
{
SharedReferences.Editor=app_preferences.edit();
编辑器.putString(“用户名”,名称);
编辑器.putString(“密码”,pass);
commit();
}
if(name.equals(“”)| pass.equals(“”)
{
Toast.makeText(Main.this,“请输入用户名和密码”,Toast.LENGTH_LONG.show();
}
其他的
试一试{
httpclient=新的DefaultHttpClient();
httppost=newhttppost(“url/login.php”);
//添加您的数据
nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“UserEmail”,name.trim());
添加(新的BasicNameValuePair(“密码”,pass.trim());
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
response=httpclient.execute(httppost);
inputStream=response.getEntity().getContent();
数据=新字节[256];
buffer=新的StringBuffer();
int len=0;
而(-1!=(len=inputStream.read(data)))
{
append(新字符串(数据,0,len));
}
inputStream.close();
}
捕获(IOE异常){
系统输出打印ln(e);
//alertDialog.cancel();
}
if(缓冲区字符(0)='Y')
{
Intent Intent=新的Intent(getApplicationContext(),另一个.class);
星触觉(意向);
}
其他的
{
System.out.println(“无效的用户名和密码”)
}
}
});
在此处添加空检查
response = httpclient.execute(httppost);
if(response != null){
inputStream = response.getEntity().getContent();
data = new byte[256];
因为当服务器连接失败时,
execute()。。正如Naresh所说,不要在MainthTred中进行服务器通信。。看看哪个会派上用场。首先,你不应该在主线程中完成你的网络任务,就像你在OnClick中做的一样
要避免连接挂起,需要设置连接超时。为了便于使用http操作,我在github中创建了一个http库,您可以将此代码用作示例引用,也可以将其用作库
同意Naresh,不要在每项任务中使用主线程。使用Service
或AsyncTask
执行长时间运行的网络任务。如果(response!=null){我使用您的编辑代码,但出现相同的错误..我能做什么请告诉methreaid=3响应信号3,将堆栈跟踪写入/data/anr/traces.txt..这是一个示例。。。