Android简单登录authen。使用MySQl和php
大家好,我正在尝试创建一个简单的登录活动,它将使用php对MySql数据库中的用户进行身份验证。。。。我有我无法解决的错误。。。请任何人帮帮我 这里是主要的活动代码Android简单登录authen。使用MySQl和php,php,android,asynchronous,task,Php,Android,Asynchronous,Task,大家好,我正在尝试创建一个简单的登录活动,它将使用php对MySql数据库中的用户进行身份验证。。。。我有我无法解决的错误。。。请任何人帮帮我 这里是主要的活动代码 public class MainActivity extends Activity { private static final String LOGIN_URL = "http://servername.com/mypath/Login.php"; EditText un , pas; Button ok; ProgressDi
public class MainActivity extends Activity {
private static final String LOGIN_URL = "http://servername.com/mypath/Login.php";
EditText un , pas;
Button ok;
ProgressDialog prog;
JSONParser jsonParser = new JSONParser();
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Idintify();
ok.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new ToLogin().execute();
}
});
}
public void Idintify(){
un = (EditText)findViewById(R.id.Un);
pas = (EditText)findViewById(R.id.Pas);
ok = (Button)findViewById(R.id.ok);
}
class ToLogin extends AsyncTask<String, String, String>{
boolean failure = false;
@Override protected void onPreExecute() {
super.onPreExecute();
prog = new ProgressDialog(MainActivity.this);
prog.setMessage("Login wait ...");
prog.setIndeterminate(false);
prog.setCancelable(true);
prog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
//int success;
int sucess;
String username = un.getText().toString();
String password = pas.getText().toString();
try {
List<NameValuePair> param = new ArrayList<NameValuePair>();
param.add(new BasicNameValuePair("username", username));
param.add(new BasicNameValuePair("password", password));
Log.d("request!", "starting");
JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL,"POST", param);
Log.d("Login attempt", json.toString());
sucess = json.getInt(TAG_SUCCESS);
if (sucess == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(MainActivity.this, Notifications.class);
finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String message) {
prog.dismiss();
if (message != null){
Toast.makeText(MainActivity.this, message, Toast.LENGTH_LONG).show();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
公共类MainActivity扩展活动{
私有静态最终字符串登录\u URL=”http://servername.com/mypath/Login.php";
联合国,pas;
按钮ok;
进程对话程序;
JSONParser JSONParser=新的JSONParser();
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_MESSAGE=“MESSAGE”;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
IDinify();
ok.setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//TODO自动生成的方法存根
新建ToLogin().execute();
}
});
}
公开无效标识(){
un=(EditText)findViewById(R.id.un);
pas=(EditText)findViewById(R.id.pas);
ok=(按钮)findViewById(R.id.ok);
}
类ToLogin扩展了异步任务{
布尔失败=假;
@重写受保护的void onPreExecute(){
super.onPreExecute();
prog=新建进度对话框(MainActivity.this);
程序设置消息(“登录等待…”);
程序设置不确定(错误);
程序设置可取消(真);
prog.show();
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
//TODO自动生成的方法存根
//成功;
成功;
字符串username=un.getText().toString();
字符串密码=pas.getText().toString();
试一试{
List param=new ArrayList();
参数add(新的BasicNameValuePair(“用户名”,用户名));
参数添加(新的BasicNameValuePair(“密码”,password));
Log.d(“请求!”,“启动”);
JSONObject json=jsonParser.makeHttpRequest(LOGIN_URL,“POST”,param);
Log.d(“登录尝试”,json.toString());
suces=json.getInt(TAG_SUCCESS);
如果(成功==1){
Log.d(“登录成功!”,json.toString();
意图i=新意图(MainActivity.this、Notifications.class);
完成();
星触觉(i);
返回json.getString(TAG_消息);
}否则{
Log.d(“登录失败!”,json.getString(TAG_MESSAGE));
返回json.getString(TAG_消息);
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
受保护的void onPostExecute(字符串消息){
程序解除();
如果(消息!=null){
Toast.makeText(MainActivity.this,message,Toast.LENGTH_LONG.show();
}
}
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(R.menu.main,menu);
返回true;
}
}
这就是JSONParser.java
public JSONParser() {
}
public JSONObject getJSONFromUrl(final String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
try {
if(method == "POST"){
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}else if(method == "GET"){
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
publicjsonparser(){
}
公共JSONObject getJSONFromUrl(最终字符串url){
试一试{
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
is.close();
json=sb.toString();
}捕获(例外e){
Log.e(“缓冲区错误”,“错误转换结果”+e.toString());
}
试一试{
jObj=新的JSONObject(json);
}捕获(JSONException e){
Log.e(“JSON解析器”,“错误解析数据”+e.toString());
}
返回jObj;
}
公共JSONObject makeHttpRequest(字符串url、字符串方法、,
列表参数){
试一试{
如果(方法==“POST”){
DefaultHttpClient httpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(参数));
HttpResponse HttpResponse=httpClient.execute(httpPost);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}else if(方法==“GET”){
DefaultHttpClient httpClient=新的DefaultHttpClient();
String paramString=URLEncodedUtils.format(params,“utf-8”);
url+=“?”+参数字符串;
HttpGet HttpGet=新的HttpGet(url);
HttpResponse HttpResponse=httpClient.execute(httpGet);
HttpEntity HttpEntity=httpResponse.getEntity();
is=httpEntity.getContent();
}
}捕获(不支持的编码异常e){
e、 printStackTrace();
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
试一试{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(
is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
斯特林
02-28 20:10:48.132: D/Login attempt(2476): {"message":"You have been sucessfully login","success":1}
02-28 20:10:48.132: D/Login Successful!(2476): {"message":"You have been sucessfully login","success":1}
02-28 20:10:48.852: I/Choreographer(2476): Skipped 43 frames! The application may be doing too much work on its main thread.
02-28 20:10:48.952: W/dalvikvm(2476): threadid=14: thread exiting with uncaught exception (group=0x40a71930)
02-28 20:10:50.375: I/Choreographer(2476): Skipped 35 frames! The application may be doing too much work on its main thread.
02-28 20:10:51.852: W/IInputConnectionWrapper(2476): showStatusIcon on inactive InputConnection
02-28 20:10:53.295: E/AndroidRuntime(2476): FATAL EXCEPTION: AsyncTask #4
02-28 20:10:53.295: E/AndroidRuntime(2476): java.lang.RuntimeException: An error occured while executing doInBackground()
02-28 20:10:53.295: E/AndroidRuntime(2476): at android.os.AsyncTask$3.done(AsyncTask.java:299)
02-28 20:10:53.295: E/AndroidRuntime(2476): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
02-28 20:10:53.295: E/AndroidRuntime(2476): at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
02-28 20:10:53.295: E/AndroidRuntime(2476): at java.util.concurrent.FutureTask.run(FutureTask.java:239)
02-28 20:10:53.295: E/AndroidRuntime(2476): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
02-28 20:10:53.295: E/AndroidRuntime(2476): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
02-28 20:10:53.295: E/AndroidRuntime(2476): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
02-28 20:10:53.295: E/AndroidRuntime(2476): at java.lang.Thread.run(Thread.java:856)
02-28 20:10:53.295: E/AndroidRuntime(2476): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.tst/com.example.tst.Notifications}; have you declared this activity in your AndroidManifest.xml?
02-28 20:10:53.295: E/AndroidRuntime(2476): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1618)
02-28 20:10:53.295: E/AndroidRuntime(2476): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1417)
02-28 20:10:53.295: E/AndroidRuntime(2476): at android.app.Activity.startActivityForResult(Activity.java:3370)
02-28 20:10:53.295: E/AndroidRuntime(2476): at android.app.Activity.startActivityForResult(Activity.java:3331)
02-28 20:10:53.295: E/AndroidRuntime(2476): at android.app.Activity.startActivity(Activity.java:3566)
02-28 20:10:53.295: E/AndroidRuntime(2476): at android.app.Activity.startActivity(Activity.java:3534)
02-28 20:10:53.295: E/AndroidRuntime(2476): at com.example.tst.MainActivity$ToLogin.doInBackground(MainActivity.java:114)
02-28 20:10:53.295: E/AndroidRuntime(2476): at com.example.tst.MainActivity$ToLogin.doInBackground(MainActivity.java:1)
02-28 20:10:53.295: E/AndroidRuntime(2476): at android.os.AsyncTask$2.call(AsyncTask.java:287)
02-28 20:10:53.295: E/AndroidRuntime(2476): at java.util.concurrent.FutureTask.run(FutureTask.java:234)
02-28 20:10:53.295: E/AndroidRuntime(2476): ... 4 more
02-28 20:10:54.352: E/WindowManager(2476): Activity com.example.tst.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d1d988 V.E..... R.....ID 0,0-640,128} that was originally added here
02-28 20:10:54.352: E/WindowManager(2476): android.view.WindowLeaked: Activity com.example.tst.MainActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{40d1d988 V.E..... R.....ID 0,0-640,128} that was originally added here
02-28 20:10:54.352: E/WindowManager(2476): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:354)
02-28 20:10:54.352: E/WindowManager(2476): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:216)
02-28 20:10:54.352: E/WindowManager(2476): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
02-28 20:10:54.352: E/WindowManager(2476): at android.app.Dialog.show(Dialog.java:281)
02-28 20:10:54.352: E/WindowManager(2476): at com.example.tst.MainActivity$ToLogin.onPreExecute(MainActivity.java:73)
02-28 20:10:54.352: E/WindowManager(2476): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
02-28 20:10:54.352: E/WindowManager(2476): at android.os.AsyncTask.execute(AsyncTask.java:534)
02-28 20:10:54.352: E/WindowManager(2476): at com.example.tst.MainActivity$1.onClick(MainActivity.java:48)
02-28 20:10:54.352: E/WindowManager(2476): at android.view.View.performClick(View.java:4204)
02-28 20:10:54.352: E/WindowManager(2476): at android.view.View$PerformClick.run(View.java:17355)
02-28 20:10:54.352: E/WindowManager(2476): at android.os.Handler.handleCallback(Handler.java:725)
02-28 20:10:54.352: E/WindowManager(2476): at android.os.Handler.dispatchMessage(Handler.java:92)
02-28 20:10:54.352: E/WindowManager(2476): at android.os.Looper.loop(Looper.java:137)
02-28 20:10:54.352: E/WindowManager(2476): at android.app.ActivityThread.main(ActivityThread.java:5041)
02-28 20:10:54.352: E/WindowManager(2476): at java.lang.reflect.Method.invokeNative(Native Method)
02-28 20:10:54.352: E/WindowManager(2476): at java.lang.reflect.Method.invoke(Method.java:511)
02-28 20:10:54.352: E/WindowManager(2476): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
02-28 20:10:54.352: E/WindowManager(2476): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
02-28 20:10:54.352: E/WindowManager(2476): at dalvik.system.NativeStart.main(Native Method)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.tst/com.example.tst.Notifications}; have you declared this activity in your AndroidManifest.xml?
if (sucess == 1) {
Log.d("Login Successful!", json.toString());
Intent i = new Intent(MainActivity.this, Notifications.class);
MainActivity.this.finish();
startActivity(i);
return json.getString(TAG_MESSAGE);
}
if (success == 1)
{
Log.d("Login Successful!", json.toString());
Intent i = new Intent(MainActivity.this, Notifications.class);
startActivity(i);
finish();
}
else
{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
}