ios json php null,PHP-在Android应用中获取jsonObject的Null值

news/2024/7/7 20:29:14

我有从json对象获取值的问题.json_encode将空字符串返回给android.

Logcat:

05-01 22:36:21.653: D/Create Response(801): {}

05-01 22:36:21.653: W/System.err(801): org.json.JSONException: No value

for success

05-01 22:36:21.663: W/System.err(801): at

org.json.JSONObject.get(JSONObject.java:354)

05-01 22:36:21.663: W/System.err(801): at

org.json.JSONObject.getInt(JSONObject.java:443)

MyPhp.php

header('Content-type=application/json; charset=utf-8');

$response = array();

// check for required fields

if (isset($_POST['B_Name']) && isset($_POST['Au_Name']) &&

isset($_POST['Pub']) && isset($_POST['Pr']) &&

isset($_POST['B_Genre'])) {

$B_Name = $_POST['B_Name'];

$Au_Name = $_POST['Au_Name'];

$Pub = $_POST['Pub'];

$Pr = $_POST['Pr'];

$B_Genre = $_POST['B_Genre'];

// include db connect class

require_once( __DIR__ . '/android/db_connect.php');

// connecting to db

$db = new DB_CONNECT();

// mysql inserting a new row

$result = mysql_query("INSERT INTO products(Book_Name, Author_Name, Book_Genre, Price, Publication) VALUES('$B_Name', '$Au_Name', '$B_Genre', '$Pr', '$Pub')");

// check if row inserted or not

if ($result) {

// successfully inserted into database

$response["success"] = 1;

$response["message"] = "Product successfully created.";

$encoded_rows = array_map('utf8_encode', $response);

echo json_encode($encoded_rows);

} else {

// failed to insert row

$response["success"] = 0;

$response["message"] = "Oops! An error occurred.";

$encoded_rows = array_map('utf8_encode', $response);

echo json_encode($encoded_rows);

}

} else {

$response["success"] = 0;

$response["message"] = "Required field(s) is missing";

$encoded_rows = array_map('utf8_encode', $response);

echo json_encode($encoded_rows);

}

这是我的doInBackground片段:

String B_Name = BookName.getText().toString();

String Au_Name = AuthorName.getText().toString();

String Pub = Publication.getText().toString();

String Pr = Price.getText().toString();

String B_Genre = BookGenre.getText().toString();

List params = new ArrayList();

params.add(new BasicNameValuePair("B_Name", B_Name));

params.add(new BasicNameValuePair("Au_Name", Au_Name));

params.add(new BasicNameValuePair("Pub", Pub));

params.add(new BasicNameValuePair("Pr", Pr));

params.add(new BasicNameValuePair("B_Genre", B_Genre));

// getting JSON Object

// Note that create product url accepts POST method

JSONObject json = jsonParser.makeHttpRequest(url_create_product,

"POST", params);

// check log cat fro response

Log.d("Create Response", json.toString());

// check for success tag

try {

int success = json.getInt(TAG_SUCCESS);

if (success == 1) {

Intent i = new Intent(getApplicationContext(),

MainActivity.class);

startActivity(i);

finish();

}

} catch (JSONException e) {

e.printStackTrace();

}

解决方法:

我没有在php上工作,但我认为echo json_encode($encoded_rows);语句仅以字符串格式输出json.检查您在哪里返回json字符串作为响应.

标签:android,php,json

来源: https://codeday.me/bug/20191013/1906202.html


http://www.niftyadmin.cn/n/2145640.html

相关文章

KNN算法之图像处理一

KNN: 1.数据挖掘分类技术中最简单的方法之一。 2.也称为邻近算法,K最近邻分类算法 3.每个样本都可以用它最接近的k个邻居来代表 4.一般,距离使用欧式距离或曼哈顿距离(通常,k≤20) python代码实现 例子&…

oracle job 调用存过,Oracle Job 调用 PROCEDURE(存储过程)实例

Oracle Job 调用 PROCEDURE(存储过程)实例1.[代码]oracle_job_procedure.sql--一:相关主要查询--查询系统存储过程select name from user_source where typePROCEDURE;--查看job 二 中 步骤4 的25是 job字段值,每个人建立时可能不同select * from sys.us…

为什么说 Java 程序员到了必须掌握 Spring Boot 的时候了?

为什么说 Java 程序员到了必须掌握 Spring Boot 的时候了?Spring Boot 2.0 的推出又激起了一阵学习 Spring Boot 热,就单从我个人的博客的访问量大幅增加就可以感受到大家对学习 Spring Boot 的热情,那么在这么多人热衷于学习 Spring Boot 之…

JDBC中数据库的事务

1.什么叫数据库事务? 事务:一组逻辑操作单元,使数据从一种状态变换到另一种状态。 一组逻辑操作单元: 一个或多个DML操作。 2.事务处理的原则: 保证所有事务都作为一个工作单元来执行,即使出现了故障,都不…

ovirt笔记:基于ovirt4.0的虚拟资源管理的权限分析

前言 虚拟资源管理平台是在ovirt4.0的基础上开发的,从原有的一个admin用户扩展成三个管理员用户和普通用户,下面简单分析一下其实现权限管理的原理。 数据库表 主要包括三张表:roles(角色表)、roles_group(角色与操作组…

linux预留的系统调用号,armlinux系统调用号和系统调用实现函数位置

armlinux系统调用号和系统调用实现函数位置系统调用序号:linux/include/asm-arm/unistd.h系统调用函数:arch/arm/kernel/calls.S对应了的文件:kernel/include/asm-generic/unistd.h可以查找关键字SYSCALL_DEFINE因为kernel/include/linux/sys…

jdbc中首次了解DAO及其子类

DAO: data(base) access object用于与数据库进行交互 1.实体类Customer.java package loey.java1.bean;import java.sql.Date;/** ORM编程思想 (object relational mapping)* 一个数据表对应一个java类* 表中的一条记录对应java类的一个对象* 表中的一…

nginx安装与部署

1:安装工具包 wget、vim和gcc yum install -y wget yum install -y vim-enhanced yum install -y make cmake gcc gcc-c 2:下载nginx安装包 wget http://nginx.org/download/nginx-1.6.2.tar.gz3:安装依赖包 yum install -y pcre pcre-devel yum install -y zlib zlib-devel …