我正在使用php和数据库mysql中的android app server端的登录活动。在登录之前,我必须在活动中以json格式显示用户名。我已经做了。但是在我的代码中有一些问题我得到了php响应,但无法登录。请
10-29 04:00:29.401: D/LightsService(381): Excessive delay setting light: 96ms
10-29 04:00:29.801: D/LightsService(381): Excessive delay setting light: 56ms
10-29 04:00:32.521: I/LatinIME:LogUtils(497): Dictionary info: dictionary = contacts.en_US.dict ; version = ? ; date = ?
10-29 04:00:33.681: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:33.681: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:33.681: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:33.681: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:33.681: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:33.691: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:33.691: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:33.691: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:33.691: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:33.691: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:33.691: E/SoundPool(381): error loading /system/media/audio/ui/KeypressStandard.ogg
10-29 04:00:33.691: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
10-29 04:00:33.691: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg
10-29 04:00:33.691: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
10-29 04:00:33.691: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg
10-29 04:00:33.691: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
10-29 04:00:33.691: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg
10-29 04:00:33.691: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
10-29 04:00:33.691: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg
10-29 04:00:33.691: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
10-29 04:00:33.691: W/AudioService(381): onLoadSoundEffects(), Error -1 while loading samples
10-29 04:00:33.791: D/InputEventConsistencyVerifier(2110): KeyEvent: ACTION_UP but key was not down.
10-29 04:00:33.791: D/InputEventConsistencyVerifier(2110): in android.widget.EditText{b1dc2668 VFED..CL .F....I. 0,209-768,288 #7f090004 app:id/password}
10-29 04:00:33.791: D/InputEventConsistencyVerifier(2110): 0: sent at 6977802000000, KeyEvent { action=ACTION_UP, keyCode=KEYCODE_TAB, scanCode=15, metaState=0, flags=0x8, repeatCount=0, eventTime=6977802, downTime=6977716, deviceId=0, source=0x101 }
10-29 04:00:39.451: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:39.451: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:39.451: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:39.451: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:39.451: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:39.451: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:39.461: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:39.461: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:39.461: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:39.461: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:39.471: E/SoundPool(381): error loading /system/media/audio/ui/KeypressStandard.ogg
10-29 04:00:39.471: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
10-29 04:00:39.471: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg
10-29 04:00:39.471: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
10-29 04:00:39.501: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg
10-29 04:00:39.501: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
10-29 04:00:39.501: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg
10-29 04:00:39.501: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
10-29 04:00:39.501: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg
10-29 04:00:39.501: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
10-29 04:00:39.511: W/AudioService(381): onLoadSoundEffects(), Error -1 while loading samples
10-29 04:00:39.581: D/dalvikvm(2110): GC_FOR_ALLOC freed 56K, 4% free 3529K/3652K, paused 22ms, total 23ms
10-29 04:00:39.591: I/dalvikvm-heap(2110): Grow heap (frag case) to 4.582MB for 1127536-byte allocation
10-29 04:00:39.621: D/dalvikvm(2110): GC_FOR_ALLOC freed 1K, 3% free 4628K/4756K, paused 28ms, total 28ms
10-29 04:00:40.451: W/SingleClientConnManager(2110): Invalid use of SingleClientConnManager: connection still allocated.
10-29 04:00:40.451: W/SingleClientConnManager(2110): Make sure to release the connection before allocating another one.
10-29 04:00:40.541: I/System.out(2110): Response : User Found{"username":"asd","password":"asd"}
10-29 04:00:40.731: I/Choreographer(2110): Skipped 43 frames! The application may be doing too much work on its main thread.
10-29 04:00:41.441: W/System.err(2110): org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject
10-29 04:00:41.531: D/dalvikvm(2110): GC_FOR_ALLOC freed 216K, 6% free 5001K/5280K, paused 85ms, total 88ms
10-29 04:00:41.541: W/System.err(2110): at org.json.JSON.typeMismatch(JSON.java:111)
10-29 04:00:41.541: W/System.err(2110): at org.json.JSONObject.<init>(JSONObject.java:159)
10-29 04:00:41.541: W/System.err(2110): at org.json.JSONObject.<init>(JSONObject.java:172)
10-29 04:00:41.541: W/System.err(2110): at com.example.helloworld.LoginForm$MyTask.onPostExecute(LoginForm.java:154)
10-29 04:00:41.541: W/System.err(2110): at com.example.helloworld.LoginForm$MyTask.onPostExecute(LoginForm.java:1)
10-29 04:00:41.541: W/System.err(2110): at android.os.AsyncTask.finish(AsyncTask.java:632)
10-29 04:00:41.541: W/System.err(2110): at android.os.AsyncTask.access$600(AsyncTask.java:177)
10-29 04:00:41.551: W/System.err(2110): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
10-29 04:00:41.551: W/System.err(2110): at android.os.Handler.dispatchMessage(Handler.java:102)
10-29 04:00:41.551: W/System.err(2110): at android.os.Looper.loop(Looper.java:136)
10-29 04:00:41.551: W/System.err(2110): at android.app.ActivityThread.main(ActivityThread.java:5017)
10-29 04:00:41.551: W/System.err(2110): at java.lang.reflect.Method.invokeNative(Native Method)
10-29 04:00:41.551: W/System.err(2110): at java.lang.reflect.Method.invoke(Method.java:515)
10-29 04:00:41.551: W/System.err(2110): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
10-29 04:00:41.551: W/System.err(2110): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
10-29 04:00:41.561: W/System.err(2110): at dalvik.system.NativeStart.main(Native Method)
10-29 04:00:41.561: I/Choreographer(2110): Skipped 84 frames! The application may be doing too much work on its main thread.
10-29 04:00:41.791: W/InputMethodManagerService(381): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@b1fbfec8 attribute=null, token = android.os.BinderProxy@b206b6d0
10-29 04:00:44.821: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:44.821: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:44.821: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:44.821: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:44.831: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:44.831: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:44.831: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:44.831: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:44.831: E/SoundPool(381): error loading /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:44.831: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/Effect_Tick.ogg
10-29 04:00:44.831: E/SoundPool(381): error loading /system/media/audio/ui/KeypressStandard.ogg
10-29 04:00:44.831: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressStandard.ogg
10-29 04:00:44.831: E/SoundPool(381): error loading /system/media/audio/ui/KeypressSpacebar.ogg
10-29 04:00:44.831: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressSpacebar.ogg
10-29 04:00:44.831: E/SoundPool(381): error loading /system/media/audio/ui/KeypressDelete.ogg
10-29 04:00:44.831: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressDelete.ogg
10-29 04:00:44.831: E/SoundPool(381): error loading /system/media/audio/ui/KeypressReturn.ogg
10-29 04:00:44.831: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressReturn.ogg
10-29 04:00:44.831: E/SoundPool(381): error loading /system/media/audio/ui/KeypressInvalid.ogg
10-29 04:00:44.831: W/AudioService(381): Soundpool could not load file: /system/media/audio/ui/KeypressInvalid.ogg
10-29 04:00:44.831: W/AudioService(381): onLoadSoundEffects(), Error -1 while loading samples
10-29 04:00:44.881: W/InputMethodManagerService(381): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@b1e82b78 attribute=null, token = android.os.BinderProxy@b206b6d0
&#13;
<?php
$hostname_localhost ="localhost";
$database_localhost ="mydatabase";
$username_localhost ="root";
$password_localhost ="";
$localhost = mysql_connect("localhost","root","","mydatabase")
or
trigger_error(mysql_error(),E_USER_ERROR);
mysql_select_db("mydatabase", $localhost);
$username = $_POST['username'];
$password = $_POST['password'];
$query_search = "select * from user where username = '".$username."' AND password = '".$password. "'";
$query_exec = mysql_query($query_search) or die(mysql_error());
$rows = mysql_num_rows($query_exec);
//echo $rows;
if($rows == 0) {
echo "No Such User Found";
}
else {
echo "User Found";
}
if($rows!=0)
{
$arr = array("username" => $username);
echo json_encode($_POST);
//print json_encode($arr);
}
?>
&#13;
package com.example.helloworld;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class LoginForm extends Activity {
Button b,bt;
EditText et,pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
ProgressDialog dialog = null;
private static final String LOGIN_URL = "http://10.0.2.2/my_folder_inside_htdocs/check.php";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
JSONperser jsonParser = new JSONperser();
b = (Button)findViewById(R.id.Button01);
et = (EditText)findViewById(R.id.username);
pass= (EditText)findViewById(R.id.password);
tv = (TextView)findViewById(R.id.tv);
bt= (Button)findViewById(R.id.Button02);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog = ProgressDialog.show(LoginForm.this, "",
"Validating user...", true);
new Thread(new Runnable() {
public void run() {
login();
}
}).start();
MyTask task = new MyTask();
task.execute("http://10.0.2.2/my_folder_inside_htdocs/check.php");
}
});
bt.setOnClickListener(new OnClickListener() {
public void onClick(View arg) {
Intent intent = new Intent(LoginForm.this, Register.class);
startActivity(intent);
}
});
}
void login(){
try{
httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://10.0.2.2/my_folder_inside_htdocs/check.php"); // make sure the url is correct.
//add your data
nameValuePairs = new ArrayList<NameValuePair>();
// Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
// e.. from here....
ResponseHandler<String> responseHandler = new BasicResponseHandler();
final String response = httpclient.execute(httppost, responseHandler);
System.out.println("Response : " + response);
runOnUiThread(new Runnable() {
public void run() {
tv.setText("Response from PHP : " + response);
dialog.dismiss();
}
});
if(response.equalsIgnoreCase("User Found")){
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(LoginForm.this,"Login Success", Toast.LENGTH_SHORT).show();
}
});
startActivity(new Intent(LoginForm.this, Userpage.class));
}
else{
showAlert();
}
}catch(Exception e){
dialog.dismiss();
System.out.println("Exception : " + e.getMessage());
}
}
public void showAlert(){
LoginForm.this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(LoginForm.this);
builder.setTitle("Login Error.");
builder.setMessage("User not Found.")
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
}
private class MyTask extends AsyncTask<String, String, String>
{
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
try {
JSONObject myJson = new JSONObject(result);
JSONObject temp = myJson.getJSONObject("arr");
Toast.makeText(getApplicationContext(), temp.getString("username"), Toast.LENGTH_LONG).show();
//JSONArray myArray = temp.getJSONArray("arr");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected String doInBackground(String... arg0) {
// TODO Auto-generated method stub
HttpClient client = new DefaultHttpClient();
HttpPost httpGet = new HttpPost(arg0[0]);
ResponseHandler<String> handler =
new BasicResponseHandler();
try {
return(client.execute(httpGet, handler));
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return "no";
}
}
}
&#13;
!救命。提前致谢。