sorry i just learning to use AsyncTask, but I am having a problem when running my code..
I have three class The first class for RestClient
public class LoginPembayaran {
private static final String urlServer = "https://webservicetagihan.herokuapp.com";
RestTemplate restTemplate = new RestTemplate();
public void login(String username, String password) throws GagalLoginException{
Map<String, String> logindata = new HashMap<>();
logindata.put("username",username);
logindata.put("password",password);
Map<String,Object> hasil = restTemplate.postForObject(urlServer + "/api/login",logindata,Map.class,new Object[]{});
if (hasil == null ){
throw new GagalLoginException("Respon valid");
}
if (!(Boolean)hasil.get("sukses")){
throw new GagalLoginException("user name / password salah");
}
}
class two for AsyncTask..
public class loginAktifity extends Fragment {
private LoginPembayaran loginPembayaran = new LoginPembayaran();
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
final View fragment = inflater.inflate(R.layout.fr_login,container,false);
Button button = (Button)fragment.findViewById(R.id.BtnLogin);
button.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = ((EditText)fragment.findViewById(R.id.Etusernama)).getText().toString();
String password = ((EditText)fragment.findViewById(R.id.EtPassword)).getText().toString();
new AsyncTask<String,Void,Boolean>(){
String ErrorMassage;
@Override
protected Boolean doInBackground(String... params) {
try {
loginPembayaran.login(params[0], params[1]);
return true;
}catch (GagalLoginException err){
ErrorMassage = err.getMessage();
return false;
}
};
@Override
protected void onPostExecute(Boolean sukses) {
if(sukses){
Intent intent = new Intent(getContext(),SesudahLoginActivity.class);
startActivity(intent);
}
else {
Toast.makeText(getContext(),ErrorMassage,Toast.LENGTH_LONG).show();
}
}
}.execute(username,password);
}
}
);
return fragment;
}
}
Class tree for Exception
public class GagalLoginException extends Exception {
public GagalLoginException(String message) {
super(message);
}
}
my logcat..
03-18 09:57:35.780 30964-31227/com.khoiron.apptagihan E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.khoiron.apptagihan, PID: 30964
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask$3.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:841)
Caused by: java.lang.NullPointerException
at com.khoiron.apptagihan.RestClient.LoginPembayaran.login(LoginPembayaran.java:31)
at com.khoiron.apptagihan.Fragment.loginAktifity$1$1.doInBackground(loginAktifity.java:48)
at com.khoiron.apptagihan.Fragment.loginAktifity$1$1.doInBackground(loginAktifity.java:41)
at android.os.AsyncTask$2.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
12q
at java.lang.Thread.run(Thread.java:841)
Please help me n Please guide me....