【问题标题】:Error Parsing data org.json.JSONException: Value 0 of type java.lang.integer cannot be converted to JSONObject解析数据 org.json.JSONException 时出错:java.lang.integer 类型的值 0 无法转换为 JSONObject
【发布时间】:2014-05-07 09:36:11
【问题描述】:

我正在尝试使用 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;
    }

    ?>

【问题讨论】:

  • 你没有返回任何 json ` echo 0;`

标签: java android mysql json login


【解决方案1】:

试试

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

在你的活动中使用这一行(加上双引号)

success = json.getInt("TAG_SUCCESS");

【讨论】:

    猜你喜欢
    • 2015-12-25
    • 2013-09-07
    • 2013-02-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-27
    相关资源
    最近更新 更多