mardi 30 décembre 2014

Can Someone Help Me On Finding The Cause Of Crash Of This Code topic

23:28






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.

MainActivity.java





Code:


package com.it4.anexsysclient;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity implements AsyncResponse {

  private EditText usernameField,passwordField;
  private TextView status,role,method;
  SigninActivity signinactivity = new SigninActivity();

 @override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      usernameField = (EditText)findViewById(R.id.editText1);
      passwordField = (EditText)findViewById(R.id.editText2);
      status = (TextView)findViewById(R.id.textView6);
      role = (TextView)findViewById(R.id.textView7);
      method = (TextView)findViewById(R.id.textView9);
      signinactivity.delegate = this;
  }
 @override
  public boolean onCreateOptionsMenu(Menu menu) {
         
          usernameField.setText("stud");
          passwordField.setText("stud");
         
      // 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();
  }

}






SigninActivity.java





Code:


package com.it4.anexsysclient;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.net.URI;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.TextView;

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);
     
      //Log.d("ADebugTag", "Value: " + result);
     
      //if(result.toString()=="student") {
              //context.startActivity(new Intent(context, GetAvailableExam.class));
      //}
  }
}






AsyncResponse.java





Code:


package com.it4.anexsysclient;

public interface AsyncResponse {
    void processFinish(String output);
}







Code:


protected void onPostExecute(String result){
      this.statusField.setText("Login Successful");
      this.roleField.setText(result);
      this.progress.dismiss();
      delegate.processFinish(result);
     
      //Log.d("ADebugTag", "Value: " + result);
     
      //if(result.toString()=="student") {
              //context.startActivity(new Intent(context, GetAvailableExam.class));
      //}
  }


The delegate.processFinish(result) is the one causing the crash, The app works fine if I try to remove/comment out it.






Written by

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

 

© 2013 Mobile Probleme. All rights resevered. Designed by Templateism

Back To Top