Android JSON org.JSON.JSONException:类型为java.lang.String的值错误无法转换为JSONObject
我知道最近很多人都解决了这个错误,但我更可能是一个初学者,我想知道我错在哪里,因为我一年前写了这段代码,现在我非常需要它:/ 提前感谢您查阅我的代码 我知道95%的解决方案是编码UTF-8而不使用BOM。但你是怎么做到的?我一直在寻找线索,似乎我一点也不明白 非常感谢你的回答Android JSON org.JSON.JSONException:类型为java.lang.String的值错误无法转换为JSONObject,android,json,Android,Json,我知道最近很多人都解决了这个错误,但我更可能是一个初学者,我想知道我错在哪里,因为我一年前写了这段代码,现在我非常需要它:/ 提前感谢您查阅我的代码 我知道95%的解决方案是编码UTF-8而不使用BOM。但你是怎么做到的?我一直在寻找线索,似乎我一点也不明白 非常感谢你的回答 package com.example.gsb; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream
package com.example.gsb;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONArray;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class GSB extends Activity {
//Login
EditText loginText, passwordText;
String login, resultat;
Button bouton;
//Praticien
TextView visiteur;
String nom_visiteur, prenom_visiteur, id_visiteur;
//
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// récupération de l'EditTextLogin grâce à son ID
loginText = (EditText) findViewById(R.id.EditTextLogin);
// récupération de l'EditTextPassword grâce à son ID
passwordText = (EditText) findViewById(R.id.EditTextPassword);
// récupération du boutton grâce à son ID
bouton = (Button) findViewById(R.id.BoutonEnvoyer);
//on applique un écouteur d'évenement au clique bouton
bouton.setOnClickListener(
new OnClickListener(){
@Override
public void onClick(View v){
//On garde le login pour l'utiliser dans la vue praticien
login = loginText.getText().toString();
try{
//Création des paramètres à envoyer au serveur web
ArrayList<NameValuePair> parametres = new ArrayList<NameValuePair>();
parametres.add(new BasicNameValuePair("login", loginText.getText().toString()));
parametres.add(new BasicNameValuePair("password", passwordText.getText().toString()));
//Envoi des paramètres aux script PHP
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://hiddenz.esy.es/gsb_android/login_verify.php");
//Envoie des données
post.setEntity(new UrlEncodedFormEntity(parametres));
//Réponse du serveur web
HttpResponse response = client.execute(post);
//Récupération de la réponse en JSON
String jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
JSONObject jsonobject = new JSONObject(jsonResult);
//On parcourt l'objet JSON pour avoir la valeur de "result"
resultat = jsonobject.getString("result");
} catch(JSONException e){
Log.e("log_tag", "Erreur JSON " + e.toString());
} catch (Exception e){
Log.e("log_tag", "Erreur connexion http " + e.toString());
}
//Si resultat JSON = ok on passe a la vue praticien sinon on affiche une erreur
if(resultat.matches("ok")){
Visiteur(v);
}else{
((TextView)findViewById(R.id.TextViewErreur)).setText("Login ou password incorrect");
}
}
}
);
}
public void Visiteur (View v){
//Affichage de la vue
setContentView(R.layout.activity_visiteur);
((TextView)findViewById(R.id.TextViewVisiteur)).setText("Bienvenue " + login);
try{
//Création des paramètres à envoyer au serveur web
ArrayList<NameValuePair> parametres = new ArrayList<NameValuePair>();
parametres.add(new BasicNameValuePair("login", login));
//Envoi des paramètres aux script PHP
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost("http://laurentlepee.com/bts/androidGSB/visiteur.php");
//Envoi des données
post.setEntity(new UrlEncodedFormEntity(parametres));
//Réponse du serveur web
HttpResponse response = client.execute(post);
//Récupération de la réponse en JSON
String jsonResult = inputStreamToString(
response.getEntity().getContent()).toString();
JSONObject jsonobject = new JSONObject(jsonResult);
//On parcourt l'objet JSON pour avoir la valeur de "result"
id_visiteur = jsonobject.getString("id_vis");
nom_visiteur = jsonobject.getString("nom_vis");
prenom_visiteur = jsonobject.getString("prenom_vis");
} catch(JSONException e){
Log.e("log_tag", "Erreur JSON " + e.toString());
} catch (Exception e){
Log.e("log_tag", "Erreur connexion http " + e.toString());
}
if (nom_visiteur.contentEquals("null")) {
} else {
((TextView)findViewById(R.id.TextViewVisiteur)).setText("Vous êtes : " + nom_visiteur + " " + prenom_visiteur + " " + id_visiteur);
}
}
private StringBuilder inputStreamToString(InputStream is) {
String rLine = "";
StringBuilder answer = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((rLine = rd.readLine()) != null) {
answer.append(rLine);
}
}
catch (IOException e) {
e.printStackTrace();
}
return answer;
}
}
哇,谢谢你们快速的回答。事实上,你说话的口气就像和一个一年没写代码的人说话一样。所以我真的不知道如何检查json,也不知道如何记录字符串。。忘了这一切:/
当我输入不正确的密码时,它会跳转到错误的登录名,再试一次,当我输入正确的密码时,它会跳转到零,导致此错误,程序会关闭。,
从您的编辑,如果密码错误,它可以正常工作,当正确登录,它失败
我知道你的问题出在服务器端
因为在正确登录时,您的服务或页面服务器端会尝试构建json,此时会出现服务器错误,导致返回一些字符串而不是json
可能是,未定义警告变量xxx
或数据库字段未找到
**要记录一些东西,请使用log.dmytag、jsonResult,您可以在logcat中看到这一点
或者使用System.out.printjsonResult;使用这种方法跟踪或记录代码并不是很好,但仍然可以做到这一点您的响应是字符串而不是json。检查JSONAPPART,您正在ui线程本身上执行网络操作,这也是错误的。您应该尝试将您试图解析的字符串记录到jsonI中。我不明白如何执行您告诉我的操作: