我的应用程序中有一个登录页面,并且我有一个服务器。每当我将选项卡设置为连接到其他无线连接并单击登录按钮时,它都会输出错误“无路由到主机”并崩溃。但是,如果我连接到我的服务器,它工作得很好。每当我在登录时连接到错误的服务器时,我都想发出提示。
这是我的代码......但我不知道把它放在哪里......请帮忙。
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();
这是我的phpRequest。
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();
}
}
}