My app does not return JSON data

Asked

Viewed 148 times

-1

I’m making an app and in a part of it the user can update the login data, just enter the screen that the data is loaded and filled, automatically. The problem is that in fact this is not happening! The data is not coming! I have already tested via url and the data appear on the web, but in the app not! Therefore?

Just follow my code:

package com.example.gustavo.domanda;

import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;

import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.Volley;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.RequestParams;
import com.loopj.android.http.TextHttpResponseHandler;

import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;

import cz.msebera.android.httpclient.Header;

public class AtualizarDadosLoginActivity extends AppCompatActivity {

private int idusuario;
private EditText nome;
private EditText sobrenome;
private EditText email;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_atualizar_dados);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    Bundle extra = getIntent().getExtras();

    if(extra != null){
       idusuario = extra.getInt("idusuario");
    }
    Toast.makeText(this, "id usuario "+idusuario, Toast.LENGTH_SHORT).show();

    getDados(idusuario);

}

private void getDados(int idusuario) {

    int opcao = 1; //mostrar dados usuário
    RequestParams rp = new RequestParams();

    AsyncHttpClient client = new AsyncHttpClient();
    client.get("http://www.reservacomdomanda.com/areaAdmin/api/admin_estabelecimento/reqDataCliJson.php?opcao="+opcao+"&idusuario="+idusuario, rp, new TextHttpResponseHandler() {
        @Override
        public void onFailure(int statusCode, Header[] headers, String responseString, Throwable throwable) {
            Toast.makeText(getBaseContext(), "Problema na conexao!"+statusCode, Toast.LENGTH_LONG).show();
        }

        @Override
        public void onSuccess(int statusCode, Header[] headers, String responseString) {
            try {
                JSONObject obj = new JSONObject(responseString);
                String retorno = "";
                Toast.makeText(AtualizarDadosLoginActivity.this, "response: "+responseString, Toast.LENGTH_SHORT).show();

                if (!obj.has("erro")) {
                    retorno += "\n" + obj.getString("idusuario");
                    retorno += "\n" + obj.getString("nome");
                    retorno += "\n" + obj.getString("sobrenome");
                    retorno += "\n" + obj.getString("email");
                    Toast.makeText(getBaseContext(),"Dados retornados: "+retorno, Toast.LENGTH_LONG).show();

                    EditText nome = (EditText) findViewById(R.id.edtNome);
                    EditText sobrenome = (EditText) findViewById(R.id.edtSobrenome);
                    EditText email = (EditText) findViewById(R.id.edtEmail);

                    nome.setText(obj.optString("nome").toString(), TextView.BufferType.EDITABLE);
                    sobrenome.setText(obj.optString("sobrenome"), TextView.BufferType.EDITABLE);
                    email.setText(obj.optString("email"), TextView.BufferType.EDITABLE);
                }

            } catch (JSONException e){

            }
        }

    });
}

}

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.example.gustavo.domanda.AtualizarDadosLoginActivity"
tools:showIn="@layout/activity_atualizar_dados">

<EditText
    android:id="@+id/edtNome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="41dp"
    android:ems="10"
    android:hint="nome"
    android:inputType="textPersonName"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintHorizontal_bias="0.503" />

<EditText
    android:id="@+id/edtSobrenome"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:hint="sobrenome"
    android:inputType="textPersonName"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/edtNome" />

<EditText
    android:id="@+id/edtEmail"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:ems="10"
    android:hint="e-mail"
    android:inputType="textPersonName"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/edtSobrenome" />

<Button
    android:id="@+id/btnAtualizar"
    android:layout_width="210dp"
    android:layout_height="wrap_content"
    android:layout_marginLeft="8dp"
    android:layout_marginRight="8dp"
    android:layout_marginTop="8dp"
    android:background="@drawable/borda_botao"
    android:text="Atualizar"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/edtEmail" />

If you run this url http://www.reservacomdomanda.com/areaAdmin/api/admin_estabelecimento/reqDataCliJson.php?opcao=1&idusuario=5 in the browser, you will see that there are data to be returned.

  • It looks like Json is coming in format array and you’re not parsing an array in your code, is that not it?

  • The value of responseString empty?

  • Do the test, run the url I passed from the API in the browser and see the format that comes. I don’t know, I didn’t test to see what comes in responseString

  • It is not easier to debug and see what comes in return?

  • Yes, the data appears in the respenseString @Lmaker: responseString[{"idusuario":"5","name":"Flavia","last name":"Severo","email":"[email protected]"}]

  • and as @Leonardodias said, the parse is incorrect.

  • And how should I do this parse?

Show 2 more comments

1 answer

0

You are trying to parse an array using an JSONObject, causing him to fall into his catch.

Try to do something like this, appropriate to your situation:

JSONArray jsonarray = new JSONArray(responseString);
for (int i = 0; i < jsonarray.length(); i++) {
  JSONObject jsonobject = jsonarray.getJSONObject(i);
  String name = jsonobject.getString("name");
  String url = jsonobject.getString("url");
}
  • It’s weird, man, because I’ve used this Jsonobject obj = new Jsonobject(responseString); and it always works! Sometimes it gives error pq the data is not in json format!

  • I made the change, and ran it in debug mode @Lmaker When this line is run: "Jsonarray jsonarray = new Jsonarray(responseString);" this appears on the console: Method threw 'java.lang.Nullpointerexception' Exception. Cannot evaluate org.json.Jsonarray.toString()

  • Put a Log in his catch and take the message from exception

  • Appears "Jsonexceptionorg.json.Jsonexception: No value for url"

  • Po brother, I just pasted the code for you to use as an example. You have to change the name of the keys to the ones in your JSON

  • Yes friend, I changed, I was not covered!!

  • Dude, I still have the same error: Method threw 'java.lang.Nullpointerexception' Exception. Cannot evaluate org.json.Jsonarray.toString()

  • Post your code after these changes please

  • Man, sorry, I don’t know how to put this code of yours with mine. Please, you can join my code of the post with this one of yours?

  • Thanks, but I managed to solve... It was another problem. My php api was wrong. It was $data[] when it should be $given.

  • So that’s why the JSONArray would work, since the $data[] sends an array.

Show 6 more comments

Browser other questions tagged

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