2014-05-07 170 views
-2

我試圖使用JSONParser爲我的應用程序創建登錄頁面,但每次單擊登錄按鈕時,錯誤只是保持未來在我的用戶名edittextbox 「顯示java.lang.NullPointerException」解析數據org.json.JSONException時出錯:java.lang.integer類型的值0無法轉換爲JSONObject

這裏是我的login.java:

public class login extends Activity{ 
    private static final String loginurl = "http://10.0.2.2/koperasidb/login.php"; 
    EditText kode,pw; 
    TextView error; 
    Button login; 
    String i; 
    private static final String TAG_SUCCESS = "success"; 
    private Session session; 
    JSONParser1 jsonParser = new JSONParser1(); 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.login); 

     session = new Session(this); 
     kode = (EditText) findViewById(R.id.kode); 
     pw = (EditText) findViewById (R.id.password); 
     login = (Button) findViewById (R.id.login); 
     error = (TextView) findViewById (R.id.error); 

     login.setOnClickListener(new View.OnClickListener(){ 
     @Override  
      public void onClick(View v) { 
      List<NameValuePair> postParameters = new ArrayList<NameValuePair>(); 
      postParameters.add(new BasicNameValuePair("kode", kode.getText().toString())); 
      postParameters.add(new BasicNameValuePair("password", pw.getText().toString())); 
      int success; 


      try { 
       JSONObject json = jsonParser.makeHttpRequest(loginurl, "GET", postParameters); 
       success = json.getInt(TAG_SUCCESS); 
       if (success ==1){ 
        error.setText("Correct Username or Password"); 
        session.setkdanggota(kode.getText().toString()); 
        berhasil(v); 
       } 
       else { 
        error.setText("Sorry!! Wrong Username or Password Entered"); 
       } 
      } 
      catch (Exception e) { 
       kode.setText(e.toString()); 
      } 
      } 
     }); 
     //Toast.makeText(this,"berhasil",3000).show(); 
    } 

      public void berhasil (View theButton) 
      { 
       Intent s = new Intent (this, Home.class); 
       startActivity(s); 
      } 
     } 

而且這裏是我的login.php:

<?php 
    include ("koneksi.php"); 
    $kd=$_POST['kode']; 
    $pw=$_POST['password']; 

    $query = "SELECT * FROM anggota_baru WHERE kd_anggota = '$kd' AND no_identitas ='$pw'"; 
    $result = mysql_query($query) or die("Unable to verify user because : " . mysql_error()); 

    if (mysql_num_rows($result) == 1){ 
     echo 1; 
     $response["success"] = 1; 
    } 
    else { 
     // print status message 
     echo 0; 
     $response["success"] = 0; 
    } 

    ?> 
+1

不必返回任何JSON'呼應0;' –

回答

1

嘗試

if (mysql_num_rows($result) == 1){ 
    echo "{\"TAG_SUCCESS\":1}"; 
}else { 
    echo "{\"TAG_SUCCESS\":0}"; 
} 

,並在活動中使用這一行(把雙引號)

success = json.getInt("TAG_SUCCESS"); 
相關問題