[英]android json get request
I want to print the textview of the user who made the android login process, I first converted the data to json format in php, but when I type database / show.php into the android url part, the textview is typed as null. 我想打印进行android登录过程的用户的textview,我首先将数据转换为php中的json格式,但是当我在android url部分中输入database / show.php时,textview被键入为null。
Php code : 邮递区号:
$id=$_GET["id"];
$sql=$conn->prepare("SELECT id,name,surname,type FROM personel WHERE id='$id'");
$sql->execute();
$result=$sql->fetch(PDO::FETCH_ASSOC);
echo (json_encode($result));
Java code : Java代码:
final String url = "database/show.php";
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest
(Request.Method.GET, url, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
if(response.equals("başarılı")) {
userId.setText(response.getString("id").toString());
userName.setText(response.getString("name"));
userSurname.setText(response.getString("surname"));
userType.setText(response.getString("type"));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "error"+error, Toast.LENGTH_SHORT).show();
}
});
// Access the RequestQueue through your singleton class. //通过单例类访问RequestQueue。
MySingleton.getInstance(this).addToRequestQueue(jsonObjectRequest); MySingleton.getInstance(本).addToRequestQueue(jsonObjectRequest);
I have some suggestions: 1. You are calling database, but id is null. 我有一些建议:1.您正在调用数据库,但id为null。 Why don't you try database/show.php?id=2 for example, and use all the url http//../database/show.php?id=2 2. Please load json file to help you a little more 例如,为什么不尝试使用database / show.php?id = 2并使用所有url http //../ database / show.php?id = 2 2.请加载json文件以帮助您更多
I see some problems with your code: 我发现您的代码存在一些问题:
1- You didn't pass any id in your new JsonObjectRequest
, So your php code doesn't GET any id probably.(should be final String url = "http://127.0.0.1/database/show.php?id=234";
) 1-您没有在new JsonObjectRequest
传递任何id,因此您的php代码可能不会获取任何id。(应该是final String url = "http://127.0.0.1/database/show.php?id=234";
)
2- You treated $id in your query as a string. 2-您将查询中的$ id视为字符串。 id is usually an Integer.(should be WHERE id=$id
) id通常是一个整数。(应为WHERE id=$id
)
3- The result of your query would be an array. 3-您的查询结果将是一个数组。 So your client would get a JSONArray, not a JSONObject.(should be response.getJSONObject(0).getString("id").toString()
) 因此,您的客户端将获得JSONArray,而不是JSONObject。(应为response.getJSONObject(0).getString("id").toString()
)
4 - And what is if(response.equals("başarılı"))
? 4-什么是if(response.equals("başarılı"))
? Comparing a JSON type with a String type? 将JSON类型与String类型进行比较?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.