Error while consuming service using Volley

Asked

Viewed 37 times

0

I am developing an application in Android Studio in which consumes a service I created in Play Framework.

This is my service route:

GET /Services/cadastrarPalheta/{matricula}/{data_instalacao}/{local}/{cod}/{estado}/{bairro}/{rua}/{complemento}/{pais}/{latitude}/{longitude}/{obs}            Services.cadastrarPalheta

My kind of service:

public class Services extends Controller {
public static void cadastrarPalheta(String matricula, String data_instalacao, String local, String cod, String estado, String bairro,
        String rua, String complemento, String pais, String latitude, String longitude) {

    if (validation.hasErrors()) {
        String mensagem = "Erro ao cadastrar";
        JsonObject j = new JsonObject();
        j.addProperty("Erro", 404);
        j.addProperty("msg", mensagem);
        renderJSON(j);
    } else {
        String msgsucess = "Cadastrado com sucesso!";
        Endereco endereco = new Endereco();
        endereco.estado = estado;
        endereco.bairro = bairro;
        endereco.local = local;
        endereco.rua = rua;
        endereco.complemento = complemento;
        endereco.pais = pais;
        endereco.latitude = latitude;
        endereco.longitude = longitude;
        endereco.save();
        Agente funcionarioBanco = Agente.find("login = ?", matricula).first();
        Palheta pal = new Palheta();
        pal.agente = funcionarioBanco;
        pal.codigo = cod;
        DateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
        try {
            pal.data_instalacao = formatter.parse(data_instalacao);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        pal.endereco = endereco;
        pal.save();
        JsonObject j = new JsonObject();
        j.addProperty("Success", 200);
        j.addProperty("msg", msgsucess);
        renderJSON(j);
    }

}

My Android app:

public class FormPalhetasActivity extends Activity implements AdapterView.OnItemSelectedListener, LocationListener {

double latitude = 0;
double longitude = 0;
public static final String TAG = "LOG";
public static final String REGISTER_URL = "http://192.168.0.108:9000/Services/cadastrarPalheta";
EditText frua, fbairro, fcompl, fcidade, festado, fpais, fobs, fcod;
public static final String KEY_USERNAME = "username";
public static final String KEY_ADDRESS = "address";
public static final String KEY_DISTRICT = "district";
public static final String KEY_COMPLEMENT = "complement";
public static final String KEY_CITY = "city";
public static final String KEY_STATE = "state";
public static final String KEY_NOTE = "note";
public static final String KEY_COD = "codigo";
public static final String KEY_LOCALE = "local";
public static final String KEY_DATE = "date";
public static final String KEY_LAT = "latitude";
public static final String KEY_LONG = "longitude";
String fmatr, flocal, fdata;
SimpleDateFormat formataData = new SimpleDateFormat("dd-MM-yyyy");
Date data = new Date();
String dataFormatada = formataData.format(data);
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate( savedInstanceState );

    setContentView( R.layout.activity_form_palhetas );
    Bundle inBundle = getIntent().getExtras();
    fmatr = inBundle.get("username").toString();
    frua = (EditText) findViewById(R.id.rua);
    fbairro = (EditText) findViewById(R.id.bairro);
    fcompl = (EditText) findViewById(R.id.compl);
    fcidade = (EditText) findViewById(R.id.cidade);
    festado = (EditText) findViewById(R.id.estado);
    fpais = (EditText) findViewById(R.id.pais);
    fobs = (EditText) findViewById(R.id.observacao);
    fcod = (EditText) findViewById(R.id.cod);

    // Spinner element
    Spinner spinner = (Spinner) findViewById(R.id.spinner);

    // Spinner click listener
    spinner.setOnItemSelectedListener(this);

    // Spinner Drop down elements
    List<String> categories = new ArrayList<String>();
    categories.add("Terreno baldio");
    categories.add("Esgoto a ceu aberto");
    categories.add("Lixo");
    categories.add("Piscina");
    categories.add("Construção");

    // Creating adapter for spinner
    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);

    // Drop down layout style - list view with radio button
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    // attaching data adapter to spinner
    spinner.setAdapter(dataAdapter);

    readMyCurrentCoordinates();
    Button enviar = (Button) findViewById( R.id.enviar );


    enviar.setOnClickListener( new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            registerForms();
        }
    } );
}
@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState( outState );
}

@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
    String item = parent.getItemAtPosition(position).toString();
    if(position == 0){
        flocal = "Terreno baldio";
    }else if(position == 1){
        flocal = "Esgoto a ceu aberto";
    }else if(position == 2){
        flocal = "Lixo";
    }else if(position == 3){
        flocal = "Piscina";
    }else if(position == 4){
        flocal = "Construção";
    }else if(position == 5){
        flocal = "Outros";
    }
}
public void registerForms() {

    final String address = frua.getText().toString().trim();
    final String district = fbairro.getText().toString().trim();
    final String city = fcidade.getText().toString().trim();
    final String complement = fcompl.getText().toString().trim();
    final String state = festado.getText().toString().trim();
    final String note = fobs.getText().toString().trim();
    final String code = fcod.getText().toString().trim();
    final String pais = fpais.getText().toString().trim();


    StringRequest stringRequest = new StringRequest( Request.Method.POST, REGISTER_URL+"?matricula="+fmatr + "&data_instalacao="+dataFormatada+"&local="+flocal
            +"&cod="+code+"&estado="+state+"&bairro="+district+"&rua="+address+"&complemento="+complement+"&pais="+pais+"&latitude="+latitude+"&longitude="+longitude
            +"&obs="+note,
            new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (response.contains("Erro")) {
                        Toast.makeText( FormPalhetasActivity.this, response, Toast.LENGTH_LONG ).show();

                    } else {
                        Intent intent = new Intent(FormPalhetasActivity.this, PrincipalActivity.class);
                        Toast.makeText( FormPalhetasActivity.this, response, Toast.LENGTH_LONG ).show();
                        startActivity(intent);
                    }
                }
            },
            new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Toast.makeText( FormPalhetasActivity.this, error.toString(), Toast.LENGTH_LONG ).show();
                    Log.i( TAG, "Lat: " + error );
                }
            } ) {
        @Override
        protected Map<String, String> getParams() {
            Map<String, String> map = new HashMap<String, String>();
            map.put( KEY_USERNAME, fmatr );
            map.put( KEY_ADDRESS, address );
            map.put( KEY_DISTRICT, district );
            map.put( KEY_COMPLEMENT, complement );
            map.put( KEY_CITY, city );
            map.put( KEY_NOTE, note );
            map.put( KEY_STATE, state );
            map.put( KEY_COD, code );
            map.put( KEY_LOCALE, flocal );
            map.put( KEY_DATE, dataFormatada );
            map.put( KEY_LAT, String.valueOf( latitude ) );
            map.put( KEY_LONG, String.valueOf( longitude ) );
            return map;
        }

    };

    RequestQueue requestQueue = Volley.newRequestQueue( this );
    requestQueue.add( stringRequest );
}
}

I am now learning to create and consume services, and am having the following error while running the application:

com.android.volley.NoConnectionError: java.io.IOException: unexpected end of stream on Connection{192.168.0.108:9000, proxy=DIRECT@ hostAddress=192.168.0.108 cipherSuite=none protocol=http/1.1} (recycle count=0)
  • try calling the address in your browser and see if you get the expected result, it may be some error on the server side.

1 answer

0


Change your route to POST and change StringRequest stringRequest = new StringRequest( Request.Method.POST, REGISTER_URL+"?matricula="+fmatr + "&data_instalacao="+dataFormatada+"&local="+flocal +"&cod="+code+"&estado="+state+"&bairro="+district+"&rua="+address+"&complemento="+complement+"&pais="+pais+"&latitude="+latitude+"&longitude="+longitude+"&obs="+note,new Response.Listener<String>()

for this:

StringRequest stringRequest = new StringRequest( Request.Method.POST, REGISTER_URL, new Response.Listener<String>()

Browser other questions tagged

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