Android,有一个空值问题
这是我的android代码中的一个问题。变量用户总是返回NULL值 实际上,这个类用于我的主页,它允许连接到用户帐户 正如我所说,我的问题在于变量user。实际上,这个变量有一个空值,然后它会改变。 顺便说一句,如果我在用英语交流问题时遇到困难,我很抱歉,这不是我的母语:)Android,有一个空值问题,android,Android,这是我的android代码中的一个问题。变量用户总是返回NULL值 实际上,这个类用于我的主页,它允许连接到用户帐户 正如我所说,我的问题在于变量user。实际上,这个变量有一个空值,然后它会改变。 顺便说一句,如果我在用英语交流问题时遇到困难,我很抱歉,这不是我的母语:) 这是我遇到问题的类的代码 这是我的长篇大论 最后,这是php代码 在实例化用户时,您是否甚至可以看到代码的一部分?这是我能想到的唯一原因。 你的产出是多少?如果您不进行一点调试(这里给出),就很难尝试查看错误的来
在实例化用户时,您是否甚至可以看到代码的一部分?这是我能想到的唯一原因。
你的产出是多少?如果您不进行一点调试(这里给出),就很难尝试查看错误的来源。我所能想到的就是你永远无法到达
user=newuser(…)
您需要带我们了解一下代码的执行过程
请为我们提供一些调试行,比如检查单击是否确实有效,以及代码执行是否真正到达doInBackground回调
祝你好运
编辑:
您将在某个时刻收到一个JSON异常。我想这就是为什么。您试图获取与JSON数组关联的键错误的JSON数组
05-29 01:04:43.881: D/Response: user(26616): > {"utilisateur":[{"CIN":"1234567","DTYPE":"Financier","PWD":"ss","PRENOM":"saif","MAIL":"hhhh","DATE":null,"NOM":"mhamdi","LOGIN":"ss","ADR":null}]}
第一个JSON对象对的键为“usilisateur”,值为JSON数组。然而,在doInBackground回调中,您试图使用不存在的不同键“user”获取完全相同的值(“user”被分配到此行中的TAG_TAB):
尝试将此行更改为“Usilisateur”,看看是否仍会出现相同的JSON异常。能否显示jsonObj和userJson.length()的值?是的,我刚刚添加了它们,请再次查看代码好吗?谢谢我用你的代码编辑了我的文章。我改成了Usilisateur,但同样的异常仍然存在。我刚刚测试了它,它通过了任何json异常,代码一直运行,直到我收到一条“valide”toast消息。请再次向我们提供详细信息。
package com.medbac.exemple.model;
public class User {
//"cin":"22","login":"rr","password":"rr","nom":"rr","prenom":"rr",
//"grade":"2","matricule_fiscal":"rr","num_carte_sejour":"2","num_tel":"888","dtype":"r"
//public String d_type;
public String cin;
public String dtype;
public String pwd;
public String prenom;
public String mail;
public String date;
public String nom;
public String login;
public String adr;
//public String grade;
public User (String cin, String dtype, String pwd, String prenom, String mail,String date,
String nom, String login, String adr){
this.cin = cin;
this.dtype = dtype;
this.pwd = pwd;
this.prenom = prenom;
this.mail = mail;
this.date = date;
this.nom = nom;
this.login = login;
this.adr=adr;
}
}
05-29 01:01:17.616: I/Timeline(26616): Timeline: Activity_idle id: android.os.BinderProxy@219ada3c time:28441294
05-29 01:01:17.616: V/ActivityThread(26616): updateVisibility : ActivityRecord{5022dc9 token=android.os.BinderProxy@219ada3c {com.medbac.exemple.activity/com.medbac.exemple.activity.TabBarMenu}} show : true
05-29 01:01:18.651: I/Timeline(26616): Timeline: Activity_idle id: android.os.BinderProxy@219ada3c time:28442332
05-29 01:03:18.096: V/ActivityThread(26616): updateVisibility : ActivityRecord{5022dc9 token=android.os.BinderProxy@219ada3c {com.medbac.exemple.activity/com.medbac.exemple.activity.TabBarMenu}} show : true
05-29 01:03:18.231: W/IInputConnectionWrapper(26616): getTextBeforeCursor on inactive InputConnection
05-29 01:03:18.241: W/IInputConnectionWrapper(26616): getCursorCapsMode on inactive InputConnection
05-29 01:03:18.271: W/IInputConnectionWrapper(26616): getCursorCapsMode on inactive InputConnection
05-29 01:03:18.436: W/IInputConnectionWrapper(26616): getTextBeforeCursor on inactive InputConnection
05-29 01:03:18.436: W/IInputConnectionWrapper(26616): getSelectedText on inactive InputConnection
05-29 01:03:18.436: W/IInputConnectionWrapper(26616): getTextAfterCursor on inactive InputConnection
05-29 01:04:35.561: I/Timeline(26616): Timeline: Activity_idle id: android.os.BinderProxy@219ada3c time:28639242
05-29 01:04:36.651: D/ViewRootImpl(26616): ViewPostImeInputStage ACTION_DOWN
05-29 01:04:38.801: D/ViewRootImpl(26616): ViewPostImeInputStage ACTION_DOWN
05-29 01:04:43.561: D/ViewRootImpl(26616): ViewPostImeInputStage ACTION_DOWN
05-29 01:04:43.616: E/url(26616): http://mf-json-app-inf.esy.es/user_hber.php?LOGIN=ss&PWD=ss
05-29 01:04:43.686: E/(26616): [android_ws] Format: 5, Width: 1002, Height: 288
05-29 01:04:43.691: E/(26616): [android_ws] Format: 5, Width: 1002, Height: 288
05-29 01:04:43.691: E/(26616): [android_ws] Format: 5, Width: 1002, Height: 288
05-29 01:04:43.691: E/(26616): [android_ws] Format: 5, Width: 1002, Height: 288
05-29 01:04:43.716: E/(26616): [android_ws] Format: 5, Width: 1080, Height: 1920
05-29 01:04:43.716: E/(26616): [android_ws] Format: 5, Width: 1080, Height: 1920
05-29 01:04:43.721: E/(26616): [android_ws] Format: 5, Width: 1002, Height: 288
05-29 01:04:43.721: E/(26616): [android_ws] Format: 5, Width: 1002, Height: 288
05-29 01:04:43.736: E/(26616): [android_ws] Format: 5, Width: 1080, Height: 1920
05-29 01:04:43.736: E/(26616): [android_ws] Format: 5, Width: 1080, Height: 1920
05-29 01:04:43.741: E/(26616): [android_ws] Format: 5, Width: 1002, Height: 288
05-29 01:04:43.741: E/(26616): [android_ws] Format: 5, Width: 1002, Height: 288
05-29 01:04:43.876: I/System.out(26616): AsyncTask #2 calls detatch()
05-29 01:04:43.881: D/Response: user(26616): > {"utilisateur":[{"CIN":"1234567","DTYPE":"Financier","PWD":"ss","PRENOM":"saif","MAIL":"hhhh","DATE":null,"NOM":"mhamdi","LOGIN":"ss","ADR":null}]}
05-29 01:04:43.881: W/System.err(26616): org.json.JSONException: No value for user
05-29 01:04:43.886: W/System.err(26616): at org.json.JSONObject.get(JSONObject.java:389)
05-29 01:04:43.886: W/System.err(26616): at org.json.JSONObject.getJSONArray(JSONObject.java:584)
05-29 01:04:43.886: W/System.err(26616): at com.medbac.exemple.activity.AcceuilFragment$GetUser.doInBackground(AcceuilFragment.java:147)
05-29 01:04:43.886: W/System.err(26616): at com.medbac.exemple.activity.AcceuilFragment$GetUser.doInBackground(AcceuilFragment.java:1)
05-29 01:04:43.886: W/System.err(26616): at android.os.AsyncTask$2.call(AsyncTask.java:288)
05-29 01:04:43.886: W/System.err(26616): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
05-29 01:04:43.886: W/System.err(26616): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
05-29 01:04:43.886: W/System.err(26616): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
05-29 01:04:43.886: W/System.err(26616): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
05-29 01:04:43.886: W/System.err(26616): at java.lang.Thread.run(Thread.java:818)
05-29 01:04:43.921: E/ViewRootImpl(26616): sendUserActionEvent() mView == null
05-29 01:04:43.936: E/(26616): [android_ws] Format: 5, Width: 1080, Height: 1920
05-29 01:04:43.936: E/(26616): [android_ws] Format: 5, Width: 1080, Height: 1920
<?php
$mm=mysqli_connect("...","...","....");
mysqli_select_db($mm,"...");
$l = $_GET['LOGIN'];
$mp = $_GET['PWD'];
$sql = mysqli_query($mm,"SELECT * FROM user WHERE LOGIN = '$l' AND PWD = '$mp'");
$tab = array();
//Retourne le nombre de lignes dans un résultat
$nb_ligne=mysqli_num_rows($sql);
if($nb_ligne >= 1){
while($ligne = mysqli_fetch_array($sql,MYSQL_ASSOC)){
//mysqli_fetch_array
//Retourne une ligne de résultat sous la forme d'un tableau associatif, d'un tableau indexé, ou les deux
//Le second argument optionnel est une constante indiquant quel type de tableau doit être renvoyé à partir de la ligne de données courante.
$tab ["utilisateur"] [] = $ligne;
} }
echo $v = str_replace('\\/','/',json_encode($tab));
//json_decode — Décode une chaîne JSON
//Récupère une chaîne encodée JSON et la convertit en une variable PHP.
mysqli_close($mm);
05-29 01:04:43.881: D/Response: user(26616): > {"utilisateur":[{"CIN":"1234567","DTYPE":"Financier","PWD":"ss","PRENOM":"saif","MAIL":"hhhh","DATE":null,"NOM":"mhamdi","LOGIN":"ss","ADR":null}]}
private static final String TAG_TAB = "user";