如何使用json响应在android中登录并打开新活动?

如何使用json响应在android中登录并打开新活动?,android,mysql,json,Android,Mysql,Json,我有一个登录活动和一个注册活动,注册活动使用jsp在我的mysql数据库中创建一个新用户,该用户使用emailid和密码。jsp非常好,用户的详细信息是json格式的。 我的json列表如下所示 http://192.110.8.4:8082/Server/user_details_json.jsp // url for the below response { Users: [ { user_id: "1", password: "123456", ema

我有一个登录活动和一个注册活动,注册活动使用jsp在我的mysql数据库中创建一个新用户,该用户使用emailid和密码。jsp非常好,用户的详细信息是json格式的。 我的json列表如下所示

http://192.110.8.4:8082/Server/user_details_json.jsp // url for the below  
     response
{
 Users: [
  {
   user_id: "1",
   password: "123456",
   email_id: "abc.arun@gmail.com"
 },
 {
   user_id: "2",
   password: "456789",
   email_id: "smart_arun@yahoo.com"
 }
 ]
}
现在我想做的是,我想使用上述任何用户详细信息登录应用程序,因此我尝试了一些代码,当我调试时,响应也正确显示,但当我单击“登录”按钮时,应用程序突然关闭,我想一次一个地获取上面的emailid和密码,并检查它是否与edittext中输入的文本匹配。 有人能建议我怎么做吗

private static String url =  
   "http://192.110.8.4:8082/Server/user_details_json.jsp";
EditText email_id, password;
btnLogin.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // Calling async task to get json
            if (email_id.getText().toString().equals("") && 
                password.getText().toString().equals("")) {
                Toast.makeText(getBaseContext(), "Login UnSuccessful", 
                Toast.LENGTH_LONG).show();
            } else {
                new GetUserDetails().execute();
            }

        }
    });

private class GetUserDetails extends AsyncTask<Void, Void, Void> {

    String user_emailId;
    String user_password;
    HashMap<String, String> contact;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        // Showing progress dialog
        pDialog = new ProgressDialog(Login.this);
        pDialog.setTitle("Contacting Servers");
        pDialog.setMessage("Logging in ...");
        pDialog.setCancelable(false);
        pDialog.show();

    }

    @Override
    protected Void doInBackground(Void... params) {
        ServiceHandler sh = new ServiceHandler();

        // Making a request to url and getting response
        String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);

        Log.d("Response: ", "> " + jsonStr);
        if (jsonStr != null) {
            try {
                JSONObject jsonObj = new JSONObject(jsonStr);

                // Getting JSON Array node
                users = jsonObj.getJSONArray(TAG_USERS);

                // looping through All Products
                for (int i = 0; i < users.length(); i++) {
                    JSONObject c = users.getJSONObject(i);

                    user_emailId = c.getString("email_id");
                    user_password = c.getString("password");

                    // tmp hashmap for single contact
                    contact = new HashMap<String, String>();

                    // adding each child node to HashMap key => value
                    contact.put(TAG_EMAIL_ID, user_emailId);
                    contact.put(TAG_PASSWORD, user_password);

                    // adding contact to contact list
                    userList.add(contact);
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            Log.e("ServiceHandler", "Couldn't get any data from the url");
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        if (pDialog.isShowing()) {
            pDialog.dismiss();
        }

        if (email_id.getText().toString().equals(user_emailId) && 
           password.getText().toString().equals(user_password)) {
            Intent in = new Intent(Login.this, MainActivity.class);
            startActivity(in);
        } else {
            Toast.makeText(getBaseContext(), "Email-id and Password does not 
            match", Toast.LENGTH_LONG).show();
        }
    }
}
私有静态字符串url=
"http://192.110.8.4:8082/Server/user_details_json.jsp";
EditText电子邮件\u id、密码;
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//调用异步任务以获取json
如果(email_id.getText().toString().equals(“”&)
password.getText().toString()等于(“”){
Toast.makeText(getBaseContext(),“登录失败”,
Toast.LENGTH_LONG).show();
}否则{
新建GetUserDetails().execute();
}
}
});
私有类GetUserDetails扩展了异步任务{
字符串user\u emailId;
字符串用户密码;
HashMap联系人;
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
//显示进度对话框
pDialog=newprogressdialog(Login.this);
pDialog.setTitle(“联系服务器”);
设置消息(“登录…”);
pDialog.setCancelable(假);
pDialog.show();
}
@凌驾
受保护的Void doInBackground(Void…参数){
ServiceHandler sh=新的ServiceHandler();
//向url发出请求并获得响应
字符串jsonStr=sh.makeServiceCall(url,ServiceHandler.GET);
Log.d(“响应:”、“>”+jsonStr);
if(jsonStr!=null){
试一试{
JSONObject jsonObj=新的JSONObject(jsonStr);
//获取JSON数组节点
users=jsonObj.getJSONArray(标记用户);
//在所有产品中循环
对于(int i=0;ivalue
联系人.put(标签\电子邮件\ ID、用户\电子邮件ID);
contact.put(标签密码、用户密码);
//将联系人添加到联系人列表
添加(联系人);
}
}捕获(JSONException e){
e、 printStackTrace();
}
}否则{
Log.e(“ServiceHandler”,“无法从url获取任何数据”);
}
返回null;
}
@凌驾
受保护的void onPostExecute(void结果){
super.onPostExecute(结果);
if(pDialog.isShowing()){
pDialog.disclose();
}
如果(email_id.getText().toString().equals(user_emailId)&&
password.getText().toString().equals(用户密码)){
Intent in=新的Intent(Login.this,MainActivity.class);
星触觉(in);
}否则{
Toast.makeText(getBaseContext(),“电子邮件id和密码不可用
匹配“,吐司。长度(长)。show();
}
}
}
我的日志:

05-08 16:59:12.583:I/global(9037):调用createSocket()返回一个新套接字。 05-08 16:59:13.133:D/回复:(9037):> 05-08 16:59:13.133:D/回答:(9037): 05-08 16:59:13.133:D/回答:(9037): 05-08 16:59:13.133:D/回答:(9037): 05-08 16:59:13.133:D/回答:(9037): 回复:(9037):{“用户”:[{“用户id”:“1”,“密码”:“123456”,“电子邮件id”:“abc”。arun@gmail.com},{“用户id”:“2”,“密码”:“456789”,“电子邮件id”:“智能”_arun@yahoo.com“}]}

05-08 16:59:13.178:E/AndroidRuntime(9037):致命异常:AsyncTask#1 05-08 16:59:13.178:E/AndroidRuntime(9037):java.lang.RuntimeException:执行doInBackground()时出错 05-08 16:59:13.178:E/AndroidRuntime(9037):在android.os.AsyncTask$3.done(AsyncTask.java:299) 05-08 16:59:13.178:E/AndroidRuntime(9037):在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 05-08 16:59:13.178:E/AndroidRuntime(9037):位于java.util.concurrent.FutureTask.setException(FutureTask.java:219) 05-08 16:59:13.178:E/AndroidRuntime(9037):在java.util.concurrent.FutureTask.run(FutureTask.java:239) 05-08 16:59:13.178:E/AndroidRuntime(9037):在android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 05-08 16:59:13.178:E/AndroidRuntime(9037):在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 05-08 16:59:13.178:E/AndroidRuntime(9037):位于java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 05-08 16:59:13.178:E/AndroidRuntime(9037):在java.lang.Thread.run(Thread.java:864) 05-08 16:59:13.178:E/AndroidRuntime(9037):由以下原因引起:java.lang.NullPointerException 05-08 16:59:13.178:E/AndroidRuntime(9037):在com.example.shoppingcart.Login$GetUserDetails.doInBackground(Login.java:219) 05-08 16:59:13.178:E/AndroidRuntime(9037):在com.example.shoppingcart.Login$GetUserDetails.doInBackground(Login.java:1) 05-08 16:59:13.178:E/AndroidRuntime(9037):在android.os.AsyncTask$2.call(AsyncTask.java:287) 05-08 16:59:13.178:E/AndroidRuntime(9037):在java.util.concurrent.FutureTask.run(FutureTask.java:234) 05-08 16:59:13.178:E/AndroidRuntime(9037):。。。4更多 05-