PHP-在Android应用中获取jsonObject的Null值

PHP-在Android应用中获取jsonObject的Null值,第1张

概述我有从json对象获取值的问题.json_encode将空字符串返回给android.Logcat:05-0122:36:21.653:D/CreateResponse(801):{}05-0122:36:21.653:W/System.err(801):org.json.JSONException:Novalueforsuccess05-0122:36:21.663:W/System.err(801):atorg.json.JSO

我有从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 valuefor success05-01 22:36:21.663: W/System.err(801):  atorg.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

<?PHPheader('Content-type=application/Json; charset=utf-8');$response = array();// check for required fIEldsif (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 classrequire_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 notif ($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值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1093426.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存