如何将 json 数组从 laravel 路由器传递到 android 工作室中的 android class
How to pass json array from laravel router to android class in android studio
我是 android studio 的新手,想为 android studio 创建 lumen api,我创建了一个简单的 android 项目,我正在为其获取列表流明 web.php 文件中的数组项并显示到 xml 列表视图。
我工作很好。
我的问题是我创建了一个简单的 laravel 流明 api,其中当 url 被击中时从 android 项目转到流明项目的 web.php 并且它 return json 响应数组。
我的 web.php 文件如下:
$router->get('/demo',function(){
$array = User::get();//variable array contain all users data
return response()->json($array);
});
我的 MainActivity 如下:
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();
}
});
}
}
我想在我的 xml 列表视图中显示这个数组,但它在模拟器上显示错误:-
"Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $"
请帮助我如何使用 android studio
处理 json 响应
试试这个
$router->get('/demo',function(){
$heroes = [
['name' => 'mahesh'], //this associative array represent your Android `Hero` mapping class
['name' => 'vishal'],
]
return response()->json($heroes);
});
如果你想从数据库中获取数据,那么你可以这样做
$heroes = User::all();
或
$heroes = User::where(...)->get();
我是 android studio 的新手,想为 android studio 创建 lumen api,我创建了一个简单的 android 项目,我正在为其获取列表流明 web.php 文件中的数组项并显示到 xml 列表视图。 我工作很好。 我的问题是我创建了一个简单的 laravel 流明 api,其中当 url 被击中时从 android 项目转到流明项目的 web.php 并且它 return json 响应数组。 我的 web.php 文件如下:
$router->get('/demo',function(){
$array = User::get();//variable array contain all users data
return response()->json($array);
});
我的 MainActivity 如下:
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();
}
});
}
}
我想在我的 xml 列表视图中显示这个数组,但它在模拟器上显示错误:- "Expected BEGIN_ARRAY but was BEGIN_OBJECT at line 1 column 2 path $"
请帮助我如何使用 android studio
处理 json 响应试试这个
$router->get('/demo',function(){
$heroes = [
['name' => 'mahesh'], //this associative array represent your Android `Hero` mapping class
['name' => 'vishal'],
]
return response()->json($heroes);
});
如果你想从数据库中获取数据,那么你可以这样做
$heroes = User::all();
或
$heroes = User::where(...)->get();