2014-05-09 108 views
0

我知道這個錯誤最近已被很多人解決,但我更可能是一個初學者,我會想知道我,因爲我一年前寫了這個代碼錯了,我很需要它現在:/JSON org.json.JSONException:值java.lang.String類型的錯誤不能轉換爲JSONObject

在此先感謝查過我的代碼,

我知道它的95%確定解決方案將編碼沒有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,既不記錄字符串..忘記所有這些:/

當我把一個不正確的密碼它跳轉到「錯誤的登錄,再試一次」,而我把良好的密碼,它跳轉到沒有任何原因的這個錯誤,程序關閉。

+1

你的反應是一個字符串不是一個JSON。檢查json – Raghunandan

+1

除此之外,你在ui線程本身上進行網絡操作也是錯誤的 – Raghunandan

+0

你應該嘗試記錄你想分析的字符串到json – meda

回答

0

好, 從你的編輯,如果密碼錯誤,它工作正常,並且當正確的登錄,它失敗。

我看到你的問題是服務器端 因爲當正確的登錄,您的服務或頁面(服務器端)嘗試建立JSON,在這一點上你的服務器錯誤,導致一些字符串,而不是返回的JSON的

它可能會警告變量XXX沒有定義

或數據庫字段沒有找到,

**登錄的東西,使用Log.d("mytag",jsonResult)你可以看到,在logcat中。

或使用System.out.print(jsonResult);它不是真的很好跟蹤或日誌使用此代碼,但仍然做的伎倆

相關問題