Error trying to make a post on a php page through Android Studio

0

Error

03 - 12 18: 44: 46.688 23858 - 23891 / com.natanielsoares.eceller E / AndroidRuntime: FATAL EXCEPTION: AsyncTask #3
Process: com.natanielsoares.eceller, PID: 23858                                                                                    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:818)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at libcore.net.UriCodec.encode(UriCodec.java:132)
at java.net.URLEncoder.encode(URLEncoder.java:57)
at com.kosalgeek.asynctask.PostResponseAsyncTask.getPostDataString(PostResponseAsyncTask.java:231)
at com.kosalgeek.asynctask.PostResponseAsyncTask.invokePost(PostResponseAsyncTask.java:179)
at com.kosalgeek.asynctask.PostResponseAsyncTask.doInBackground(PostResponseAsyncTask.java:158)
at com.kosalgeek.asynctask.PostResponseAsyncTask.doInBackground(PostResponseAsyncTask.java:31)
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) 
at java.lang.Thread.run(Thread.java:818) 

My code

package com.natanielsoares.eceller.WebService;

import android.content.Context;
import android.widget.Toast;

import com.kosalgeek.asynctask.AsyncResponse;
import com.kosalgeek.asynctask.PostResponseAsyncTask;
import com.natanielsoares.eceller.Objetos.Usuario;

import java.util.HashMap;

/**
 * Created by Nataniel on 12/03/2016.
 */
public class UsuarioWS implements  AsyncResponse
{
    String url = "http://192.168.0.104:7070/meusprojetos/webService";
    private Context context;

    public void setContext(Context context) {
        this.context = context;
    }

    public void enviaCadastroUsuario( Usuario usuario){

        HashMap postData = new HashMap();
        postData.put("nome",usuario.getNome());
        postData.put("login",usuario.getLogin());
        postData.put("senha",usuario.getSenha());
        postData.put("cpf",usuario.getCpf());
        postData.put("email",usuario.getEmail());
        postData.put("numero",String.valueOf(usuario.getNumero()));
        postData.put("rua",usuario.getEndereco());
        postData.put("bairro",usuario.getEndereco());
        postData.put("idCidade",String.valueOf(usuario.getIdCidade()));

        PostResponseAsyncTask task = new PostResponseAsyncTask(context,  postData, this);

        task.execute(url+"/cadastraUsuario.php");
    }

    @Override
    public void processFinish(String result) {
        Toast.makeText(context, result, Toast.LENGTH_LONG).show();
    }
}
    
asked by anonymous 12.03.2016 / 22:57

1 answer

1

The error occurs because it is likely that some postData HasMap parameter is null!

The stack says that the error occurs in

  

at   com.kosalgeek.asynctask.PostResponseAsyncTask.getPostDataString (PostResponseAsyncTask.java:231)

The line 231 is the following action:

result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));

If you call the URLEncoder.encode method with a null parameter it pops a NullPointerException on the following line:

StringBuffer out = new StringBuffer(s.length());

Try to validate each parameter before adding it in HasMap as follows:

 if(null != usuario.getLogin()){
   postData.put("login",usuario.getLogin());
}
 if(null != usuario.getSenha()){
   postData.put("login", usuario.getSenha());
}

This you should do for all fields!

    
15.03.2016 / 18:42