【问题标题】:Retrieve Json object in android using volley使用 volley 在 android 中检索 Json 对象
【发布时间】:2018-04-16 13:50:46
【问题描述】:

尝试从我的 php api 在 android 上使用 volley 获取一个值...我的 api 工作正常,返回我想要的值,但在我的 android 上,我似乎可以将值保存在变量上。

这是我的 php 脚本

<?php

if($_SERVER['REQUEST_METHOD']=='POST'){

$titu = $_POST['titulo'];
$id_use = $_POST['id_user'];
$difi = $_POST['dificuldade'];


$user = "root";
$pass = "";
$host= "localhost";
$dbname="check";

$con = mysqli_connect($host,$user,$pass,$dbname);
$sql="insert into trilhos(titulo,id_user,dificuldade) 
values('".$titu."','".$id_use."','".$difi."');";


$r = mysqli_query($con,$sql);

$id_trilho =mysqli_insert_id($con);

$result = array();
echo json_encode(array("result"=>$id_trilho));

mysqli_close($con);
}

比在我的 android studio 上我尝试获取这样的值,我发送 3 个值插入到数据库中,然后我想将我插入的行的 id 返回到 android。我在 stackoverflow 上查看了很多答案,但我似乎无法理解和解决我的问题。

public void insert(String titulo, String id_trilho, String dif) {
    url = "http://192.168.1.68/loginsignup/insertTrilho.php?titulo=" + titulo +"&id_user="+ id_trilho+ "&dificuldade=" + dif + "";
    Log.i("Http", "" + url);
    RequestQueue requestQueue = Volley.newRequestQueue(InserirTrilho.this);
    StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
        @Override
        public void onResponse(String response) {
            try {
                JSONObject jsonObject = new JSONObject(response);
                JSONArray jsonArray = jsonObject.getJSONArray("result");
                JSONObject jsonObject1 = jsonArray.getJSONObject(0);
                String id = jsonObject1.getString("id_trilho");
                Toast.makeText(InserirTrilho.this, id, Toast.LENGTH_SHORT).show();


                SharedPreferences shared_id = getSharedPreferences("Mytrilho", Context.MODE_PRIVATE);
                SharedPreferences.Editor editor = shared_id.edit();
                editor.putString("id", id);


                editor.commit();
                Intent intent = new Intent(InserirTrilho.this, MapsActivity.class);
                startActivity(intent);

            } catch (JSONException e) {
                e.printStackTrace();
                Toast.makeText(InserirTrilho.this, "Dados Errados", Toast.LENGTH_SHORT).show();
            }
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.i("HttpError","erro"+error);
        }

    });
    requestQueue.add(stringRequest);
}

不断抛出异常,谁能帮帮我,提前谢谢你

【问题讨论】:

  • 用这个jsonObject.getString("id_trilho");代替jsonObject1.getString("id_trilho");
  • 这行代码json_encode(array("result"=&gt;$id_trilho));不会给你你认为你试图解析的JSON结构!请更新您的问题并提供您返回的 JSON 响应示例。我很难相信你没有收到错误。
  • Pavneet_Singh 解决了我的问题,由于某种原因我无法编辑我的答案以便其他人可以正确看到它,但他的回答解决了我的问题

标签: php android mysql json android-volley


【解决方案1】:

问题是,url 是 Get 请求的类型,但 java 中的方法是 POST 类型的请求,因此您需要使用 Body 发送数据作为请求的一部分,所以

see how to send a POST request using volley with string body?


As commented , 你的回复是jsonobject 不是数组所以使用

JSONObject jsonObject = new JSONObject(response);
String id = jsonObject.optString("result");    

而不是

JSONObject jsonObject = new JSONObject(response);
JSONArray jsonArray = jsonObject.getJSONArray("result");
JSONObject jsonObject1 = jsonArray.getJSONObject(0);
String id = jsonObject1.getString("id_trilho");

或者得到你想要的然后使用

$result = array();
$result["result"]= array();
array_push($result["result"],array("id_trilho"=>$id_trilho)); // assume $id_trilho =0
echo json_encode($result);
// output {"result":[{"id_trilho":0}]}

或者干脆用

$id_trilho = 0;
$result = array();
array_push($result,array("id_trilho"=>$id_trilho));
echo json_encode($result);
// output [{"id_trilho":0}]
// then use JSONArray jsonArray = new JSONArray(response);
// JSONObject jsonObject1 = jsonArray.getJSONObject(0);
// String id = jsonObject1.getString("id_trilho");

【讨论】:

  • 感谢您的回答,我已经尝试了您所说的一切,但我一直收到相同的错误 org.json.JSONException: Value
  • @Sapo121 您的 sql 查询中存在错误,因此请纠正其语法,如答案所示
  • 每次我使用邮递员测试脚本我都会得到一个结果,它会在我的数据库上插入一个新行,在我的安卓上我无法得到响应
  • @Sapo121 使用这个JSONObject jsonObject = new JSONObject(response); String id = jsonObject.optString("result");
  • {"result":[{"id_trilho":48}]}
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-01-26
相关资源
最近更新 更多