PHP-在Android应用中获取jsonObject的Null值
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了PHP-在Android应用中获取jsonObject的Null值,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2890字,纯文字阅读大概需要5分钟。
内容图文
我有从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
<?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<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();
}
解决方法:
似乎问题出在PHP代码上. check echo method of php.
我没有在php上工作,但我认为echo json_encode($encoded_rows);语句仅以字符串格式输出json.检查您在哪里返回json字符串作为响应.
内容总结
以上是互联网集市为您收集整理的PHP-在Android应用中获取jsonObject的Null值全部内容,希望文章能够帮你解决PHP-在Android应用中获取jsonObject的Null值所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。