Java 在安卓系统中,当无法连接到互联网时,如何放置对话框?
我的应用程序中有一个登录页面,我有一个服务器。每当我将选项卡设置为连接到其他无线连接并单击“登录”按钮时,它就会输出一个错误“无主机路由”并崩溃。但是,如果我连接到我的服务器,它工作得非常好。每当我在登录时连接到错误的服务器时,我都想发出提示 这是我的密码。。。但我不知道该把这个放在哪里。。请帮忙Java 在安卓系统中,当无法连接到互联网时,如何放置对话框?,java,android,httpresponse,Java,Android,Httpresponse,我的应用程序中有一个登录页面,我有一个服务器。每当我将选项卡设置为连接到其他无线连接并单击“登录”按钮时,它就会输出一个错误“无主机路由”并崩溃。但是,如果我连接到我的服务器,它工作得非常好。每当我在登录时连接到错误的服务器时,我都想发出提示 这是我的密码。。。但我不知道该把这个放在哪里。。请帮忙 AlertDialog.Builder builder = new AlertDialog.Builder(LoginPage.this); builder.setTi
AlertDialog.Builder builder = new AlertDialog.Builder(LoginPage.this);
builder.setTitle("Attention!");
builder.setMessage("Connection to Server failed.");
builder.setPositiveButton("Retry", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
new phpRequest().execute();
}
});
builder.setNegativeButton("Cancel", null);
AlertDialog dialog = builder.create();
dialog.show();
这是我的请求
private class phpRequest extends AsyncTask<String, Integer, String>
{
@Override
protected String doInBackground(String... params)
{
String responseString = "";
HttpClient httpclient = new DefaultHttpClient();
HttpPost request = new HttpPost(myurl);
try
{
String studSt = etStudNo.getText().toString();
String passSt = etPassword.getText().toString();
List<NameValuePair> parameter = new ArrayList<NameValuePair>();
parameter.add(new BasicNameValuePair("student_id", studSt));
parameter.add(new BasicNameValuePair("password", passSt));
request.setEntity(new UrlEncodedFormEntity(parameter));
HttpResponse response = httpclient.execute(request);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK)
{
ByteArrayOutputStream out = new ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString = out.toString();
}
else
{
response.getEntity().getContent().close();
throw new IOException(statusLine.getReasonPhrase());
}
}
catch (Exception ioe)
{
Log.e("Error", ioe.toString());
}
return responseString;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
String c = result;
int check = Integer.parseInt(c);
if (check == 1)
{
Intent i = new Intent(LoginPage.this,HomePage.class);
startActivity(i);
globalVars globalVars = (globalVars)getApplicationContext();
String idnumber = etStudNo.getText().toString();
globalVars.setStudLoggedId(idnumber);
Toast.makeText(getBaseContext(), "Student No: "+etStudNo.getText().toString(),Toast.LENGTH_LONG).show();
}
else
{
etPassword.setText("");
Toast.makeText(getBaseContext(), "Login Failed", Toast.LENGTH_LONG).show();
}
}
}
私有类phpRequest扩展异步任务
{
@凌驾
受保护的字符串doInBackground(字符串…参数)
{
字符串responseString=“”;
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost请求=新的HttpPost(myurl);
尝试
{
字符串studSt=etStudNo.getText().toString();
字符串passSt=etPassword.getText().toString();
列表参数=新的ArrayList();
添加(新的BasicNameValuePair(“学生id”,studSt));
添加(新的BasicNameValuePair(“密码”,passSt));
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse response=httpclient.execute(请求);
StatusLine StatusLine=response.getStatusLine();
if(statusLine.getStatusCode()==HttpStatus.SC\u OK)
{
ByteArrayOutputStream out=新建ByteArrayOutputStream();
response.getEntity().writeTo(out);
out.close();
responseString=out.toString();
}
其他的
{
response.getEntity().getContent().close();
抛出新IOException(statusLine.getReasonPhrase());
}
}
捕获(异常ioe)
{
Log.e(“Error”,ioe.toString());
}
回报率;
}
@凌驾
受保护的void onPostExecute(字符串结果)
{
super.onPostExecute(结果);
字符串c=结果;
int check=Integer.parseInt(c);
如果(检查==1)
{
意向i=新意向(LoginPage.this,HomePage.class);
星触觉(i);
globalVars globalVars=(globalVars)getApplicationContext();
字符串idnumber=etStudNo.getText().toString();
globalVars.setStudLoggedId(idnumber);
Toast.makeText(getBaseContext(),“学生号:”+etStudNo.getText().toString(),Toast.LENGTH_LONG.show();
}
其他的
{
etPassword.setText(“”);
Toast.makeText(getBaseContext(),“登录失败”,Toast.LENGTH_LONG.show();
}
}
}
问题从这里开始:
HttpResponse response = httpclient.execute(request);
因为如果无法连接到服务器并完成请求,它将抛出一个IOException
然后在此处捕获异常:
catch (Exception ioe)
{
Log.e("Error", ioe.toString());
}
并且您的函数将继续,但是responseString
仍然包含您第一次分配给它的空字符串“”,而不是HTTP响应(因为它从未完成)
这意味着返回空字符串“”,并将其传递给onPostExecute(字符串结果)
,其中Integer.parseInt(c)
将失败并引发NumberFormatException
,这可能是导致应用程序崩溃的原因
一个简单的解决办法是:
doInBackground()
中的异常处理程序中,记录错误后,应返回null
以指示方法失败onPostExecute(字符串结果)
中,检查结果是否为null
,然后再执行其他操作。如果是,您知道请求失败,您可以安全地弹出一个描述错误的对话框希望有帮助 你想要什么?当应用程序崩溃时?任何日志在崩溃时也会发布应用程序日志