![](/img/trans.png)
[英]Error parsing data org.json.JSONException: Value <!— of type java.lang.String cannot be converted to JSONObject
[英]JSON org.json.JSONException: Value Error of type java.lang.String cannot be converted to JSONObject
我知道这个错误最近已经被很多人解决了,但是我很可能是一个初学者,而且我想知道自从我一年前编写这段代码以来哪里出错了,现在我很需要它: /
预先感谢您查询我的代码,
我知道百分之九十五的解决方案可以在没有BOM的情况下对UTF-8进行编码。 但是,你是怎么做的? 我一直在寻找线索,看来我根本听不懂...。
真的谢谢你的回答。
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.d("mytag",jsonResult )
在logcat中可以看到。
或使用System.out.print(jsonResult );
使用它跟踪或记录代码不是很好,但是仍然可以解决问题
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.