我正在为我的应用制作登录表单。成功整合api后,我试图检索我的电子邮件和密码以进行登录验证,但我正在使用的过程没有获取数据,因为我尝试log.v
检查所需数据是否存储在变量或不
Json数据示例:
{
"Email": "shayam@gmail.com",
"Password": xxcctt,
"Name": "shayam",
"Phone": "987654321"
}
Login.java
package com.example.android.bet;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class Login extends AppCompatActivity {
private EditText username;
private EditText password;
private Button login;
Context context;
public static final String MyPREFERENCES = "Usergrant" ;
public static final String User = "userKey";
public static final String Pass = "passKey";
String uid,upass;
SharedPreferences sharedpreferences;
SharedPreferences.Editor editor;
private final static String restURL="XXXXXXX";
Message msg = new Message();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
context=this;
setupVariables();
}
Handler handler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
if(msg.arg1==1)
{
Toast.makeText(context, "email id or password invalid", Toast.LENGTH_SHORT).show();
//Print Toast or open dialog
}
return false;
}
});
private class CustomTask extends AsyncTask<Void, Void, Void> {
protected Void doInBackground(Void... param) {
String apiurl=restURL+"xxxEmail="+uid+"&Password="+upass;
Log.v("URL Generated", apiurl);
String betJsonStr=null;
try {
HttpURLConnection urlConnection = null;
BufferedReader reader = null;
try {
URL url = new URL(apiurl);
urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
StringBuffer buffer = new StringBuffer();
reader = new BufferedReader(new InputStreamReader(inputStream));
String line;
while ((line = reader.readLine()) != null) {
buffer.append(line + "\n");
}
betJsonStr = buffer.toString();
Log.v("json",betJsonStr);
} catch (IOException e) {
Log.e("Didn't get data", "Error", e);
}
try {
JSONObject jObject = new JSONObject(betJsonStr);
String email,pasw;
email=jObject.optString("Email").toString();
pasw= jObject.optString("Dealer").toString();
Log.v("email and",email+" "+pasw);
if(uid.equals(email)&&upass.equals(pasw))
{
sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);
editor = sharedpreferences.edit();
editor.putString(User, uid);
editor.putString(Pass, upass);
editor.commit();
Intent menu=new Intent(context,MainActivity.class);
startActivity(menu);
}
else
{
msg.arg1=1;
handler.sendMessage(msg);
}
}
catch (JSONException e) {
e.printStackTrace();
}
}
catch (Exception e) {
Log.e("Exception arised", "error", e);
}
//Do some work
return null;
}
protected void onPostExecute(Void param) {
//Print Toast or open dialog
}
}
public void authenticateLogin(View view)
{
uid=username.getText().toString();
upass=password.getText().toString();
new CustomTask().execute((Void[])null);
}
private void setupVariables()
{
username=(EditText) findViewById(R.id.username);
password=(EditText) findViewById(R.id.password);
login=(Button) findViewById(R.id.login);
username.setText("");
password.setText("");
}
}