Doubt Volley on Android

Asked

Viewed 38 times

0

Is it necessary to have a Listview to send data to the webservice with Volley on Android? Because I only created three Edittext to test. It’s returning "timeout error".

I’ve checked the url, it’s correct. I’ve been trying to do this for days, I’m not getting it. That’s why I’m resorting to help here.

I’ll post all the code here:

public class MainActivity extends AppCompatActivity implements Response.ErrorListener, Response.Listener<JSONObject> {
    private EditText codigoCurso, nomeCurso, categoriaCurso, professorCurso;
    private Button botaoCadastrar;
    //Essa variavel sera utiliziada para solicitar as requisiçõies via Json
    RequestQueue request;
    //Serve para armazenar os dados
    JsonObjectRequest jsonObjectReq;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        codigoCurso = findViewById(R.id.editCodigo);
        nomeCurso = findViewById(R.id.editNomeCurso);
        categoriaCurso =findViewById(R.id.editCategoria);
        professorCurso = findViewById(R.id.editProfessor);
        botaoCadastrar = findViewById(R.id.btCadastrar);

        //Instanciando o objeto e informando que ele receberá asrequisições do Volley.
        request = Volley.newRequestQueue(this);

    }

    //Todo método que é chamado por um button precisar ter View como parametro
    public void carregarWebService(View view) {

        String url = "http://192.168.xxx.x/webservices/registro.php?nome="+ nomeCurso.getText().toString() +"&categoria="+ categoriaCurso.getText().toString() +"&professor="+ professorCurso.getText().toString() + "" ;

        //é necessário substituir os espaços em branco que vem na url, senão da erro
        url = url.replace(" ", "%20");

        /* Lendo a url */
        jsonObjectReq = new JsonObjectRequest(Request.Method.GET,url,null,this,this);

            request.add(jsonObjectReq);


    }

    @Override
    public void onErrorResponse(VolleyError error) {
        Toast.makeText(this,"Não foi possível conectar ao servidor",Toast.LENGTH_SHORT).show();
    }

    @Override
    public void onResponse(JSONObject response) {
        Toast.makeText(this,"Cadastrado com sucesso!",Toast.LENGTH_SHORT).show();
        codigoCurso.setText("");
        nomeCurso.setText("");
        categoriaCurso.setText("");
        professorCurso.setText("");
        botaoCadastrar.setText("");
    }
}

I believe this line may be in error:

jsonObjectReq = new JsonObjectRequest(Request.Method.GET,url,null,this,this);

'Cause I don’t have a list in my form, just Edittex. It can be done that way?

I manually tested the url the webservice is working normally.

  • Where is the web service on your local machine? The application is running on a real device or emulator?

  • The webservice is running on xampp, with php, on my notebook. I am testing on my mobile connected to the usb cable.

  • The phone must be connected to the same network, and the access ip must be the ipv4 of your notebook.

  • I took ipv4 from my pc, I’ve been noticing some tutorials on realized that after ipv4, some people use the port , type 8080. That is in the url, soon after informing ipv4, it is inserted like this: ":8080". That would be what is missing?

  • I suggest you use some tool to test your server before, where you can insert the ip, port, pass all parameters to then replicate in your Android application. One widely used software for this is Postman (https://www.getpostman.com/), very easy to use.

  • I already tested, including it is running on the online server, the problem is in offline mode (localhost)

  • As is the return of your request, would post here?

Show 2 more comments
No answers

Browser other questions tagged

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