I have a login page in my app and i have a server. whenever i set my tab to connect to other wireless connection and click the login button, it outputs an error “No route to host” and crashes. but, if i connect to my server it works perfectly fine. i want to put a prompt whenever i connected to the wrong server on login.
here’s my code… but i don’t know where to put this.. pls help.
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();
here’s my 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();
}
}
}
The problem starts here:
as this is going to throw an
IOExceptionif it can’t connect to the server and complete the request.You then catch the exception here:
and your function continues, but
responseStringstill contains the empty string “” that you first assigned to it, not the HTTP response (as it never completed).This means the empty string “” gets returned and passed to
onPostExecute(String result), whereInteger.parseInt(c)will fail and throw aNumberFormatExceptionwhich is probably what is crashing your app.A simple solution would be:
doInBackground(), after logging the error you shouldreturn nullto indicate the method failed.onPostExecute(String result), check if result isnullbefore doing anything else. If it is, you know the request failed and you can safely pop up a dialog box describing the error.If you put a few breakpoints into Eclipse you can follow this through yourself and understand it properly.
Hope that helps!