java - 如何修复 com.android.volley.ParseError: org.json.JSONExeption: Value
我有一个 listview 的自定义适配器,我想从数据库中获取项目列表并使用 json 从 php 构建 CustomArrayAdapter,但问题是我从
问题描述
我有一个 listview 的自定义适配器,我想从数据库中获取项目列表并使用 json 从 php 构建 CustomArrayAdapter,但问题是我从函数 onErrorResponse 获取 VolleyError 错误。
我通过传递 burger 和 $_POST['category'] 使用邮递员发送了一个帖子请求,并且我得到了这种格式:
{
"items": [
{
"title": "Beef Burger",
"price": "10"
},
{
"title": "Pug Burger",
"price": "12"
},
{
"title": "Green-Chile Burger with Fried Eggs",
"price": "15"
},
{
"title": "Minetta Burger",
"price": "8"
},
{
"title": "Cheese Burger",
"price": "13"
}
]
}
<?php
$connection = mysqli_connect("localhost", "root", "", "mydb");
if(!$connection){
die("Connection Failure");
}
else{
$category = $_POST['category'];
$response['items'] = array();
$sql = "SELECT * FROM item WHERE category = '$category'";
$result = mysqli_query($connection, $sql);
if((mysqli_num_rows($result)) > 0){
while($rows = mysqli_fetch_assoc($result)){
$item['title'] = $rows['title'];
$item['price'] = $rows['price'];
array_push($response['items'], $item);
}
}
header('Content-Type: application/json');
echo json_encode($response);
mysqli_close($connection);
}
?>
public class Item extends AppCompatActivity {
List<DataInformation> informationList;
RequestQueue requestQueue;
ListView listView;
String URL = "http://192.168.137.1/MyMobileProject/getItem.php";
DataInformationAdapter adapter;
String category;//the post string
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.itemlayout);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
category = bundle.getString("category");//can be burger-pizza-drinks
}
informationList = new ArrayList<>();
requestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();
JSONArray items = response.getJSONArray("items");
for(int i = 0; i < items.length(); i++){
JSONObject item = items.getJSONObject(i);
String s1 = item.getString("title");
int s2 = item.getInt("price");
DataInformation dataInformation = new DataInformation(s1, s2);
informationList.add(dataInformation);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("category", category);//category value is taken from intent
return params;
}
};
requestQueue.add(jsonObjectRequest);
adapter = new DataInformationAdapter(Item.this, R.layout.itemlayout2, informationList);
listView = (ListView)findViewById(R.id.list);
listView.setAdapter(adapter);
}
}
List informationList 应该从 php json 编码构建,然后通过 CustomAdapter 并构建 listview。
"compilerOptions": {
"allowJs": true
}
那条线就可以了。
解决方案
推荐阅读
- codeigniter - CodeIgniter getMethod() 已弃用且提交按钮不起作用
- audio - ffmpeg:如何重新采样音频文件
- javascript - 如何在铯resium反应组件上加载kml文件
- python - Pycharm、Homebrew 和 Pip3 的 Python 模块错误
- xamarin - 如何跨平台包含松散文件
- javascript - 为什么简单的 for 循环会使我的计算机运行缓慢?
- javascript - 无法读取购物车组件未定义的属性“长度”
- python - 使用 wxmplot 交互式模块放大绘图后如何访问数据?
- function - 如何运行 AutoIt 脚本并从中调用函数?
- .net - Serilog 正在为指向一个物理文件路径的两个端点创建两个日志文件
我有一个 listview 的自定义适配器,我想从数据库中获取项目列表并使用 json 从 php 构建 CustomArrayAdapter,但问题是我从
问题描述
我有一个 listview 的自定义适配器,我想从数据库中获取项目列表并使用 json 从 php 构建 CustomArrayAdapter,但问题是我从函数 onErrorResponse 获取 VolleyError 错误。
我通过传递 burger 和 $_POST['category'] 使用邮递员发送了一个帖子请求,并且我得到了这种格式:
{
"items": [
{
"title": "Beef Burger",
"price": "10"
},
{
"title": "Pug Burger",
"price": "12"
},
{
"title": "Green-Chile Burger with Fried Eggs",
"price": "15"
},
{
"title": "Minetta Burger",
"price": "8"
},
{
"title": "Cheese Burger",
"price": "13"
}
]
}
<?php
$connection = mysqli_connect("localhost", "root", "", "mydb");
if(!$connection){
die("Connection Failure");
}
else{
$category = $_POST['category'];
$response['items'] = array();
$sql = "SELECT * FROM item WHERE category = '$category'";
$result = mysqli_query($connection, $sql);
if((mysqli_num_rows($result)) > 0){
while($rows = mysqli_fetch_assoc($result)){
$item['title'] = $rows['title'];
$item['price'] = $rows['price'];
array_push($response['items'], $item);
}
}
header('Content-Type: application/json');
echo json_encode($response);
mysqli_close($connection);
}
?>
public class Item extends AppCompatActivity {
List<DataInformation> informationList;
RequestQueue requestQueue;
ListView listView;
String URL = "http://192.168.137.1/MyMobileProject/getItem.php";
DataInformationAdapter adapter;
String category;//the post string
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.itemlayout);
Bundle bundle = getIntent().getExtras();
if(bundle != null) {
category = bundle.getString("category");//can be burger-pizza-drinks
}
informationList = new ArrayList<>();
requestQueue = Volley.newRequestQueue(getApplicationContext());
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
Toast.makeText(getApplicationContext(), "hello", Toast.LENGTH_SHORT).show();
JSONArray items = response.getJSONArray("items");
for(int i = 0; i < items.length(); i++){
JSONObject item = items.getJSONObject(i);
String s1 = item.getString("title");
int s2 = item.getInt("price");
DataInformation dataInformation = new DataInformation(s1, s2);
informationList.add(dataInformation);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), error.toString(), Toast.LENGTH_SHORT).show();
}
}){
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("category", category);//category value is taken from intent
return params;
}
};
requestQueue.add(jsonObjectRequest);
adapter = new DataInformationAdapter(Item.this, R.layout.itemlayout2, informationList);
listView = (ListView)findViewById(R.id.list);
listView.setAdapter(adapter);
}
}
List informationList 应该从 php json 编码构建,然后通过 CustomAdapter 并构建 listview。
"compilerOptions": {
"allowJs": true
}
那条线就可以了。