Ram Agrawal
Ram Agrawal

Reputation: 99

How to pass json array from laravel router to android class in android studio

I am new in android studio and want to create lumen api for android studio, I created an simple android project for which i am getting list of array item from lumen web.php file and show to xml listview. I work very well. My problem is i created a simple laravel lumen api in which from android project when url is hit it goes to web.php of lumen project and it return json response with an array. my web.php file as follow:

$router->get('/demo',function(){
		 $array = User::get();//variable array contain all users data
		return response()->json($array);
});

and my MainActivity as follow:

package com.example.ram.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class MainActivity extends AppCompatActivity {
    ListView listView;

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

        listView = (ListView) findViewById(R.id.listViewHeroes);

        //calling the method to display the heroes
        getHeroes();
    }

    private void getHeroes() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Api.BASE_URL)
                .addConverterFactory(GsonConverterFactory.create()) //Here we are using the GsonConverterFactory to directly convert json data to object
                .build();

        Api api = retrofit.create(Api.class);

        Call<List<Hero>> call = api.getHeroes();

        call.enqueue(new Callback<List<Hero>>() {
            @Override
            public void onResponse(Call<List<Hero>> call, Response<List<Hero>> response) {

                List<Hero> heroList = response.body();

                //Creating an String array for the ListView
                String[] heroes = new String[heroList.size()];

                //looping through all the heroes and inserting the names inside the string array
                for (int i = 0; i < heroList.size(); i++) {
                    heroes[i] = heroList.get(i).getName();
                }


                //displaying the string array into listview
                listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, heroes));

            }

            @Override
            public void onFailure(Call<List<Hero>> call, Throwable t) {
                Toast.makeText(getApplicationContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

}

I want to show this array in my xml listview but it show me error on Emulator as:- "Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $"

Please help me how to handle json response with android studio

Upvotes: 0

Views: 722

Answers (1)

rkj
rkj

Reputation: 8287

try this

$router->get('/demo',function(){
         $heroes = [
                   ['name' => 'mahesh'],  //this associative array represent your Android `Hero` mapping class
                   ['name' => 'vishal'],
                  ]
        return response()->json($heroes);
});

if you want to fetch data from database then you can do it like this

$heroes = User::all();

OR

$heroes = User::where(...)->get();

Upvotes: 1

Related Questions