对于获得的JSONObject的Android应用程序Null值 [英] getting Null value for jsonObject in android app
本文介绍了对于获得的JSONObject的Android应用程序Null值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有问题,得到的JSON对象.json_en code返回空字符串值到Android。
logcat的:
05-01 22:36:21.653:D /创建响应(801):{}
05-01 22:36:21.653:W / System.err的(801):org.json.JSONException:无值
成功
05-01 22:36:21.663:W / System.err的(801):在
org.json.JSONObject.get(JSONObject.java:354)
05-01 22:36:21.663:W / System.err的(801):在
org.json.JSONObject.getInt(JSONObject.java:443)
MyPhp.php
< PHP
标题(内容类型=应用程序/ JSON的;字符集= UTF-8);
$响应=阵列();
//检查所需的字段
如果(使用isset($ _ POST ['B_Name'])及和放大器;使用isset($ _ POST ['Au_Name'])及和放大器;
使用isset($ _ POST ['酒吧'])及和放大器;使用isset($ _ POST ['镨'])及和放大器;
使用isset($ _ POST ['B_Genre'])){
$ B_Name = $ _ POST ['B_Name'];
$ Au_Name = $ _ POST ['Au_Name'];
$酒馆= $ _ POST ['酒吧'];
$ PR = $ _ POST ['镨'];
$ B_Genre = $ _ POST ['B_Genre'];
//其中包括DB连接类
require_once(__DIR__/android/db_connect.php');
//连接到数据库
$ DB =新DB_CONNECT();
// MySQL的插入新行
$结果= mysql_query(INSERT INTO产品(BOOK_NAME,AUTHOR_NAME,Book_Genre,价格,出版)VALUES('$ B_Name','$ Au_Name','$ B_Genre','$镨','$酒吧'));
//检查是否行插入或不
如果($结果){
//成功插入到数据库
$响应[成功] = 1;
$响应[信息] =产品成功创建。;
$ EN coded_rows = array_map('utf8_en code',$响应);
回声json_en code($ EN coded_rows);
} 其他 {
//没有插入行
$响应[成功] = 0;
$响应[信息] =!哎呀发生了错误。
$ EN coded_rows = array_map('utf8_en code',$响应);
回声json_en code($ EN coded_rows);
}
} 其他 {
$响应[成功] = 0;
$响应[信息] =必填字段(S)丢失;
$ EN coded_rows = array_map('utf8_en code',$响应);
回声json_en code($ EN coded_rows);
}
这里是我的一片doInBackground的:
字符串B_Name = BookName.getText()的toString()。
字符串Au_Name = AuthorName.getText()的toString()。
。串酒吧= Publication.getText()的toString();
字符串PR = Price.getText()的toString()。
字符串B_Genre = BookGenre.getText()的toString()。
名单<的NameValuePair> PARAMS =新的ArrayList<的NameValuePair>();
params.add(新BasicNameValuePair(B_Name,B_Name));
params.add(新BasicNameValuePair(Au_Name,Au_Name));
params.add(新BasicNameValuePair(酒吧,酒吧));
params.add(新BasicNameValuePair(公关,镨));
params.add(新BasicNameValuePair(B_Genre,B_Genre));
//获取JSON对象
//注意,创造产品的URL接受POST方法
JSONObject的JSON = jsonParser.makeHtt prequest(url_create_product,
POST,则params);
//检查日志猫来回响应
Log.d(创建回应,json.toString());
//检查成功标签
尝试 {
INT成功= json.getInt(TAG_SUCCESS);
如果(成功== 1){
意图I =新的意图(getApplicationContext()
MainActivity.class);
startActivity(ⅰ);
完();
}
}赶上(JSONException E){
e.printStackTrace();
}
解决方案
好像问题是用PHP code。 检查PHP 回声方法。
我还没有工作的PHP的,但我认为回声json_en code($ EN coded_rows);
语句简单地打印JSON字符串格式。检查要返回JSON字符串作为响应。
I have problem with getting value from json object .json_encode return null string to 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
<?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);
}
And here is my piece of 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<NameValuePair> params = new ArrayList<NameValuePair>();
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();
}
解决方案
Seems issue is with PHP code. check echo method of php.
I have not worked on php but I think echo json_encode($encoded_rows);
statement simply prints json in string format. Check where you are returning json string in response.
这篇关于对于获得的JSONObject的Android应用程序Null值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文