错误解析数据org.json.JSONException:java.lang.integer类型的值0无法转换为JSONObject

时间:2014-05-07 09:36:11

标签: java android mysql json login

我正在尝试使用JSONParser为我的应用创建一个登录页面,但每次我点击登录按钮时,错误就会继续出现在我的用户名edittextbox“Java.lang.NullPointerException”

这是我的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 个答案:

答案 0 :(得分:1)

尝试

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

并在您的活动中使用此行(放双引号)

success = json.getInt("TAG_SUCCESS");