-3

Unfortunately android application has been stopped. At Http Post while attempting to call server at post activity please help

HttpClient cli = new DefaultHttpClient();
            //HttpPost post = new HttpPost("http://" + sp.getString("ip", "localhost") + "/attendance/cliLogin.php");

            HttpPost post = new HttpPost("localhost/attendance/");

            // seting post data
            List<NameValuePair> loginData = new ArrayList<NameValuePair>(2);
            loginData.add(new BasicNameValuePair("uname", uname));
            loginData.add(new BasicNameValuePair("pass", pass));
            post.setEntity(new UrlEncodedFormEntity(loginData));

            // executing login
            HttpResponse res = cli.execute(post);
                         HttpEntity resent = res.getEntity();

                            String result = EntityUtils.toString(resent);


            // reading response

            if(result.equals("NoParams"))
                Commons.showToast("Something went wrong", true);
            else if(result.equals("Login"))
            {
                navi = new Intent(this, HomeActivity.class);
                startActivity(navi);
            }
            else
                Commons.showToast(result, true);
        }
        catch (HttpHostConnectException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            Commons.showToast("Can't reach server, check the Hostname", true);
        } catch (ParseException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    else
        Commons.showToast("Username/Password can't be empty", true);
}
}
Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

0

Could you please share your logcat to see the error? I think that your are calling the php script in your main thread (that is your activity thread) and you may have NetworkOnMainThreadException

If that is the case: Create a class that extends the AsyncTask. Override its methods. In the creator of this class pass the variables and assign them to the fields (variables) of your class.

Make your post in an AsyncTask inside the doInBackground method And call the AsyncTask execute. Like this:

public class MyTask extends AsyncTask<String,String,String>{
  private short errorType = -1;
  private String result;
  private String uname;
  private String pass;
  public MyTask(String uname, String pass){
       this.uname = uname;
       this.pass = pass;
 }

@Override
protected String onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
if (errorType == 1){
   Commons.showToast("Can't reach server, check the Hostname", true); 
 }
if(result.equals("NoParams")) {      
    Commons.showToast("Something went wrong", true); }
 else if(result.equals("Login")) { 
      Intent navi = new Intent(this, HomeActivity.class);         startActivity(navi);
  } 
else {
       Commons.showToast(result, true);
   }
 }
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
 try
    {


   HttpClient cli = new DefaultHttpClient();


       HttpPost post = new HttpPost("localhost/attendance/");
  List<NameValuePair> loginData = new      ArrayList<NameValuePair>(2);    
 loginData.add(new     BasicNameValuePair("uname", uname));
 loginData.add(new BasicNameValuePair("pass", pass));
 post.setEntity(new UrlEncodedFormEntity(loginData)); 
// executing login
 HttpResponse res = cli.execute(post); 
  HttpEntity resent = res.getEntity();
 return result = EntityUtils.toString(resent); 

     }
catch (HttpHostConnectException e) 
    {
     // TODO Auto-generated catch block         
     e.printStackTrace();      
      errorType = 1;
       return null;
  } 
catch (ParseException e) {
 // TODO Auto-generated catch block            e.printStackTrace(); 
 errorType = 2;
 return null;
} 
catch (IOException e) { 
// TODO Auto-generated catch block     e.printStackTrace(); 
 errorType = 3;
  return null;
}
     catch(Exception e){
         errorType = 4;
         return null;
     }
}
}

And inside the activity make your call like this:

MyTask new MyTask(uname, pass).execute();

Again. All this is applicable only if you have

NetworkOnMainThreadException

Please share your logcat for further help.

mantlabs
  • 352
  • 2
  • 6