URL_STRING does not fill correctametne

Asked

Viewed 97 times

0

I have a problem in the consumer class of my Webservice Rest. In the project there are only two classes, Mainactivity is working correctly, but the consumption class (Consumows) is not understanding my method to fill the url/Uri that is sent to WS.

Mainactivity:

package com.example.testedosbotoes;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity{

ConsomeWS post;
String nome;
String enderecoOI = "El/oi";
String enderecoOO = "El/oo";
static String enderecoGO;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        Button bt09 = (Button) findViewById(R.id.id_btnovehoras);
        Button bt10 = (Button) findViewById(R.id.id_btdezhoras);



        bt09.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                MainActivity.setendereco(enderecoOI);


                  post=new ConsomeWS();
                  post.execute();
                nome = new String (post.getnome());
                if (nome !=null) {

                Toast.makeText(MainActivity.this, "começou", Toast.LENGTH_LONG).show();        

                }

            }

        });

        bt10.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {

                MainActivity.setendereco(enderecoOO);
                post=new ConsomeWS();
                post.execute();
                nome = new String (post.getnome());
                if (nome !=null) {

            } else {
                Toast.makeText(MainActivity.this, "nao começou", Toast.LENGTH_LONG).show(); 
            }
        };
        });


    }

    public static void setendereco (String endereco){

        enderecoGO = endereco;

    }

    public static String getendereco() {


        return enderecoGO;
    }

}

Consumiws:

package com.example.testedosbotoes;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;

import org.apache.http.HttpEntity;
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 org.json.JSONException;
import org.json.JSONObject;

import android.content.Intent;
import android.os.AsyncTask;
import android.util.Log;

public class ConsomeWS extends AsyncTask<Void, Void, String>{


    static String endereco;
    private static final String URL_STRING = "http://192.168.1.5:8080/"+endereco;

    String nome;

    @Override
    protected String doInBackground(Void... params) {

        endereco = MainActivity.getendereco();

                Log.i("hhhhhhhhhhhhhhhhhh", "solicitar endereco" + endereco);

        try {

            String resultado = consultaServidor();

            return interpretaresultado(resultado);





        } catch (IOException e) {

            e.printStackTrace();
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            return null;
        }
        return null;
    }



        private String consultaServidor() throws IOException {

            InputStream is = null;

            try {
                HttpClient httpclient = new DefaultHttpClient();

                Log.i("IIIIIIIIIIIIIIIIIIIIIIIII", "solicitar endereco" + URL_STRING);

                HttpGet httpget = new HttpGet(URL_STRING);

                Log.i("JJJJJJJJJJJJJJJJJJJJJJ", "solicitar endereco" + URL_STRING);

                HttpResponse response = httpclient.execute(httpget);

                HttpEntity entity = response.getEntity();

                InputStream webs = entity.getContent();

                Reader reader = new InputStreamReader(webs);
                char[] buffer = new char[3999];
                reader.read(buffer);

                return new String(buffer);


                } finally {

                    if (is !=null){

                        is.close();
                    }

                }
        }

            private String interpretaresultado(String resultado) throws JSONException {

                JSONObject object = new JSONObject(resultado);

                return object.getString("resposta");

        }

            @Override
            protected void onPostExecute(String result) {



                setNome(result);

            }

            public void setNome(String result) {

                this.nome = result;

            }

            public String getnome(){

                return this.nome;
            }

}

Logcat error:

08-09 13:47:32.052: D/gralloc_goldfish(2570): Emulator without GPU emulation detected.
08-09 13:47:58.742: D/AndroidRuntime(2570): Shutting down VM
08-09 13:47:58.742: W/dalvikvm(2570): threadid=1: thread exiting with uncaught exception (group=0xb4a87ba8)
08-09 13:47:58.752: E/AndroidRuntime(2570): FATAL EXCEPTION: main
08-09 13:47:58.752: E/AndroidRuntime(2570): Process: com.example.testedosbotoes, PID: 2570
08-09 13:47:58.752: E/AndroidRuntime(2570): java.lang.NullPointerException
08-09 13:47:58.752: E/AndroidRuntime(2570):     at java.lang.String.<init>(String.java:443)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at com.example.testedosbotoes.MainActivity$1.onClick(MainActivity.java:39)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at android.view.View.performClick(View.java:4438)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at android.view.View$PerformClick.run(View.java:18422)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at android.os.Handler.handleCallback(Handler.java:733)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at android.os.Handler.dispatchMessage(Handler.java:95)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at android.os.Looper.loop(Looper.java:136)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at android.app.ActivityThread.main(ActivityThread.java:5017)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at java.lang.reflect.Method.invokeNative(Native Method)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at java.lang.reflect.Method.invoke(Method.java:515)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-09 13:47:58.752: E/AndroidRuntime(2570):     at dalvik.system.NativeStart.main(Native Method)
08-09 13:47:58.762: I/hhhhhhhhhhhhhhhhhh(2570): solicitar enderecoEl/oi
08-09 13:47:58.762: I/IIIIIIIIIIIIIIIIIIIIIIIII(2570): solicitar enderecohttp://192.168.1.5:8080/null
08-09 13:47:59.112: I/JJJJJJJJJJJJJJJJJJJJJJ(2570): solicitar enderecohttp://192.168.1.5:8080/null
08-09 13:48:00.582: I/Process(2570): Sending signal. PID: 2570 SIG: 9

Note that the logcat points out that URL_STRING is incomplete T_T (192.168.1.5:8080/null) when it should appear (192.168.1.5:8080/El/oi).

1 answer

1

Solved!

I made the following change in the Class, and the/:

public class ConsomeWS extends AsyncTask<Void, Void, String>{

private static final String URL_STRING = "http://192.168.1.5:8080/"+endereco(); 

String nome;

private static String endereco() {
    String ender;
    ender = MainActivity.getendereco();
    Log.i("IIIIIIIIIIIIIIIIIIIIIIIII", "e ai" + ender);
    return ender;
}

@Override
protected String doInBackground(Void... params) {       

    try {

        String resultado = consultaServidor();          
        return interpretaresultado(resultado);      

    } catch (IOException e) {

        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
    return null;
}

    private String consultaServidor() throws IOException {

        InputStream is = null;

        try {
            HttpClient httpclient = new DefaultHttpClient();

            HttpGet httpget = new HttpGet(URL_STRING);

            Log.i("JJJJJJJJJJJJJJJJJJJJJJ", "solicitar endereco" + URL_STRING);

            HttpResponse response = httpclient.execute(httpget);

            HttpEntity entity = response.getEntity();

            InputStream webs = entity.getContent();

            Reader reader = new InputStreamReader(webs);
            char[] buffer = new char[3999];
            reader.read(buffer);

            return new String(buffer);

            } finally {

                if (is !=null){

                    is.close();
                }

            }
    }

        private String interpretaresultado(String resultado) throws JSONException {

            JSONObject object = new JSONObject(resultado);

            return object.getString("resposta");

    }

        @Override
        protected void onPostExecute(String result) {

            setNome(result);

        }

        public void setNome(String result) {

            this.nome = result;

        }

        public String getnome(){

            return this.nome;
        }

}

Logcat: 08-10 11:28:25.476: I/JJJJJJJJJJJJJJJJJJJJJJ(995): solicitar enderecohttp://192.168.1.5:8080/El/oi

XD. Only now you have another T_T error

Browser other questions tagged

You are not signed in. Login or sign up in order to post.