I'm trying to get the result of the asynctask by using an interface but when I've tried to use the methods on that interface my app keeps on crashing.
This project has 4 java files: MainActivity.java , SigninActivity.java , GetAvailableExam.java , AsyncResponse.java
I'm pretty sure the error is not on the GetAvailableExam.java because it is just an activity that prints hello world so I'm not going to post it's code here.
// Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } public void login(View view){ String username = usernameField.getText().toString(); String password = passwordField.getText().toString(); method.setText("Get Method"); new SigninActivity(this,status,role,0).execute(username,password);
} public void loginPost(View view){ String username = usernameField.getText().toString(); String password = passwordField.getText().toString(); method.setText("Post Method"); new SigninActivity(this,status,role,1).execute(username,password);
}
public void processFinish(String output){ //Toast.makeText(getApplicationContext(), output, Toast.LENGTH_LONG).show(); }
public class SigninActivity extends AsyncTask<String,Void,String>{
private TextView statusField,roleField; private Context context; private int byGetOrPost = 0; private String server_ip; private ProgressDialog progress; public AsyncResponse delegate=null;
public SigninActivity() {
}
//flag 0 means get and 1 means post.(By default it is get.) public SigninActivity(Context context,TextView statusField, TextView roleField,int flag) { this.context = context; this.statusField = statusField; this.roleField = roleField; byGetOrPost = flag; this.progress = new ProgressDialog(context); }
protected void onPreExecute(){ server_ip = "http://192.168.0.101/anexsys/examination-manager.php"; this.progress.setMessage("Logging in"); this.progress.show(); } @override protected String doInBackground(String... arg0) { if(byGetOrPost == 0){ //means by Get Method try{ String username = (String)arg0[0]; String password = (String)arg0[1]; String link = "http://myphpmysqlweb.hostei.com/login.php?username=" +username+"&password="+password; URL url = new URL(link); HttpClient client = new DefaultHttpClient(); HttpGet request = new HttpGet(); request.setURI(new URI(link)); HttpResponse response = client.execute(request); BufferedReader in = new BufferedReader (new InputStreamReader(response.getEntity().getContent()));
StringBuffer sb = new StringBuffer(""); String line=""; while ((line = in.readLine()) != null) { sb.append(line); break; } in.close(); return sb.toString(); }catch(Exception e){ return new String("Exception: " + e.getMessage()); } } else{ try{ String username = (String)arg0[0]; String password = (String)arg0[1]; String link=server_ip; String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8"); data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8"); URL url = new URL(link); URLConnection conn = url.openConnection(); conn.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter (conn.getOutputStream()); wr.write( data ); wr.flush(); BufferedReader reader = new BufferedReader (new InputStreamReader(conn.getInputStream())); StringBuilder sb = new StringBuilder(); String line = null; // Read Server Response while((line = reader.readLine()) != null) { sb.append(line); break; } return sb.toString(); }catch(Exception e){ return new String("Exception: " + e.getMessage()); } } } @override protected void onPostExecute(String result){ this.statusField.setText("Login Successful"); this.roleField.setText(result); this.progress.dismiss(); delegate.processFinish(result);
We are Creative Blogger Theme Wavers which provides user friendly, effective and easy to use themes.
Each support has free and providing HD support screen casting.
0 commentaires:
Enregistrer un commentaire