error when using webservice in android studio

4

I'm trying to login to a MySQL database and when you press the button to enter the application it stops.

This is all the JAVA code I'm using:

package com.example.chvid.servicioandroid;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import org.json.JSONArray;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {

    Button btnIngresar;
    EditText edusu,edpas;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        edusu=(EditText)findViewById(R.id.edtusu);
        edpas=(EditText)findViewById(R.id.edtpas);
        btnIngresar=(Button)findViewById(R.id.btningresar);

        btnIngresar.setOnClickListener(this);


    }


    @Override
    public void onClick(View v) {
        Thread tr=new Thread(){
            @Override
            public void run() {
                final String resultado=enviarDatosGET(edusu.getText().toString(),edpas.getText().toString());
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        int r=obtDatosJSON(resultado);
                        if (r>0){
                            Intent i=new Intent(getApplicationContext(),registro_nota.class);
                            i.putExtra("cod",edusu.getText().toString());
                            startActivity(i);
                        }else {
                            Toast.makeText(getApplicationContext(),"Usuario o Pass incorrecto",Toast.LENGTH_LONG).show();
                        }
                    }
                });
            }
        };
                tr.start();
    }


    public String enviarDatosGET(String usu, String pas){
        URL url=null;
        String linea="";
        int respuesta=0;
        StringBuilder resul=null;
        try {
            url=new URL("http://192.168.1.34/ServiciosWEB/valida.php?usu="+usu+"&pas="+pas);
            HttpURLConnection conection=(HttpURLConnection)url.openConnection();
            respuesta=conection.getResponseCode();

            resul=new StringBuilder();

            if(respuesta==HttpURLConnection.HTTP_OK){
                InputStream in=new BufferedInputStream(conection.getInputStream());
                BufferedReader reader=new BufferedReader(new InputStreamReader(in));

                while ((linea=reader.readLine())!=null){
                    resul.append(linea);
                }

            }


        }catch (Exception e){}

        return resul.toString();
    }

    public int obtDatosJSON(String response){
        int res=0;
        try {
            JSONArray json=new JSONArray(response);
            if (json.length()>0){
                res=1;

            }
        }catch (Exception e){}
        return res;
    }



}

This is what it prints me:

    06-03 08:53:28.412 2714-2714/com.example.chvid.servicioandroid I/art: Not late-enabling -Xcheck:jni (already on)
06-03 08:53:28.450 2714-2720/com.example.chvid.servicioandroid E/art: Failed writing handshake bytes (-1 of 14): Broken pipe
06-03 08:53:28.450 2714-2720/com.example.chvid.servicioandroid I/art: Debugger is no longer active
06-03 08:53:28.492 2714-2714/com.example.chvid.servicioandroid W/System: ClassLoader referenced unknown path: /data/app/com.example.chvid.servicioandroid-1/lib/x86
06-03 08:53:33.008 2714-2714/com.example.chvid.servicioandroid W/System: ClassLoader referenced unknown path: /data/app/com.example.chvid.servicioandroid-1/lib/x86
06-03 08:53:33.196 2714-2714/com.example.chvid.servicioandroid W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
06-03 08:53:33.237 2714-2823/com.example.chvid.servicioandroid D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true

                                                                                 [ 06-03 08:53:33.248  2714: 2714 D/         ]
                                                                                 HostConnection::get() New Host Connection established 0xaff49240, tid 2714


                                                                                 [ 06-03 08:53:33.291  2714: 2823 D/         ]
                                                                                 HostConnection::get() New Host Connection established 0xaaa35d30, tid 2823
06-03 08:53:33.293 2714-2823/com.example.chvid.servicioandroid I/OpenGLRenderer: Initialized EGL, version 1.4
06-03 08:54:41.699 2714-3844/com.example.chvid.servicioandroid E/AndroidRuntime: FATAL EXCEPTION: Thread-131
                                                                                 Process: com.example.chvid.servicioandroid, PID: 2714
                                                                                 java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.StringBuilder.toString()' on a null object reference
                                                                                     at com.example.chvid.servicioandroid.MainActivity.enviarDatosGET(MainActivity.java:89)
                                                                                     at com.example.chvid.servicioandroid.MainActivity$1.run(MainActivity.java:44)
06-03 08:54:42.113 2714-2823/com.example.chvid.servicioandroid E/Surface: getSlotFromBufferLocked: unknown buffer: 0xb4096ec0
06-03 08:54:42.115 2714-2823/com.example.chvid.servicioandroid D/OpenGLRenderer: endAllStagingAnimators on 0xae4e2580 (RippleDrawable) with handle 0xa24bee00

                                                                                 [ 06-03 08:54:42.143  1533: 3852 D/         ]
                                                                                 HostConnection::get() New Host Connection established 0x9a80c450, tid 3852
06-03 08:54:44.001 2714-3844/com.example.chvid.servicioandroid I/Process: Sending signal. PID: 2714 SIG: 9

They will excuse me, but I am new to android and I do not know how to solve it.

--- I just solved it, the only thing I needed was this code fragment, placing inside the manifest

<uses-sdk
        android:minSdkVersion="3"
        android:targetSdkVersion="15" />
    <uses-permission android:name="android.permission.INTERNET"/>
    
asked by Raphael 03.06.2016 в 11:02
source

1 answer

2

The error you have and causes your application to close is:

  

java.lang.NullPointerException: Attempt to invoke virtual method   'java.lang.String java.lang.StringBuilder.toString ()' on a null object   reference
  at   com.example.chvid.servicioandroid.MainActivity.enviarDatosGET (MainActivity.java:89)

and it is within this method, the result of the variable resul is null, therefore the error is obtained when you want to call the toString() method of a null instance:

public String enviarDatosGET(String usu, String pas){
        URL url=null;
        String linea="";
        int respuesta=0;
        StringBuilder resul=null;
        try {
            url=new URL("http://192.168.1.34/ServiciosWEB/valida.php?usu="+usu+"&pas="+pas);
            HttpURLConnection conection=(HttpURLConnection)url.openConnection();
            respuesta=conection.getResponseCode();

            resul=new StringBuilder();

            if(respuesta==HttpURLConnection.HTTP_OK){
                InputStream in=new BufferedInputStream(conection.getInputStream());
                BufferedReader reader=new BufferedReader(new InputStreamReader(in));

                while ((linea=reader.readLine())!=null){
                    resul.append(linea);
                }

            }


        }catch (Exception e){}

        return resul.toString();
    }

The result value is null because there is a problem in the Web Service to make the connection, I recommend initializing the StringBuilder before consulting the WS:

...
...
   StringBuilder resul = new StringBuilder();;
        try {               
            url=new URL("http://192.168.1.34/ServiciosWEB/valida.php?usu="+usu+"&pas="+pas);
            HttpURLConnection conection=(HttpURLConnection)url.openConnection();
            respuesta=conection.getResponseCode();
...
...

and verify the reason why your WS does not get results.

    
answered by 03.06.2016 / 14:21
source