android和php mysql之间的连接返回null和empty
我有这段代码,它使用Asynctask和json在android和php之间创建连接,以获取响应,但问题是响应总是空的或空的 我知道问题是什么: 字符串标记_NAME是在php文件中必须相同的JSON节点 此字符串始终提供“用户名”,不接受用户输入 所以要修正这个错误?? 有人能帮我吗?我会很感激的 AndroidPpConnectionDemo package pack.coderzheavenandroid和php mysql之间的连接返回null和empty,php,android,android-asynctask,Php,Android,Android Asynctask,我有这段代码,它使用Asynctask和json在android和php之间创建连接,以获取响应,但问题是响应总是空的或空的 我知道问题是什么: 字符串标记_NAME是在php文件中必须相同的JSON节点 此字符串始终提供“用户名”,不接受用户输入 所以要修正这个错误?? 有人能帮我吗?我会很感激的 AndroidPpConnectionDemo package pack.coderzheaven import java.util.ArrayList; import java.u
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
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 AndroidPHPConnectionDemo extends Activity {
Button b;
EditText et, pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
private static String pid;
private static String Username;
private static String Password;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single person url
// ******************************************************************
// the localhost in the google android emulator = 10.0.2.2
// the localhost in the genymotion emulator = 10.0.3.2
// ******************************************************************
private static final String url_check_login = "http://10.0.3.2/check.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PERSON = "person";
// private static final String TAG_PID = "pid";
private static String TAG_NAME = "Username";
private static String TAG_pass = "password";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.Button01);
et = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
tv = (TextView) findViewById(R.id.tv);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Getting complete person details in background thread
new CheckLogin().execute();
}
});
}
/**
* Background Async Task to Get complete person details
* */
class CheckLogin extends AsyncTask<String, String, String> {
JSONArray productObj;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AndroidPHPConnectionDemo.this);
pDialog.setMessage("Loading person details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting person details in background thread
* */
@Override
protected String doInBackground(String... arg0) {
// updating UI from Background Thread
// Check for success tag
// Username = et.getText().toString();
// Log.e("username in create method", " the username is " +
// Username);
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", Username));
// getting person details by making HTTP request
// Note that person details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(url_check_login,
"GET", params);
// Log.e("JsonObject", json.toString());
// check your log for json response
// Log.d("Single person Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received person details
productObj = json.getJSONArray(TAG_PERSON); // JSON Array
// TAG_NAME = et.getText().toString();
// Log.e("username in create method", " the username is " + TAG_NAME);
}
else {
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
if (productObj != null) {
try {
// get first product object from JSON Array
JSONObject person = productObj.getJSONObject(0);
et.setText(person.getString(Username));
//TAG_NAME = Username;
pass.setText(person.getString(TAG_pass));
Toast.makeText(
getBaseContext(),
et.getText().toString() + pass.getText().toString(),
Toast.LENGTH_LONG).show();
Log.e("success in login", "SUCCESS IN LOGIN");
} catch (Exception e) {
e.printStackTrace();
}
}
Log.e("after the post execute", " THE USERNAME IS " + Username);
pDialog.dismiss();
}
}
}
import java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TextView;
导入android.widget.Toast;
公共类AndroidHPConnectionDemo扩展活动{
按钮b;
编辑文本,通过;
文本视图电视;
HttpPost-HttpPost;
字符串缓冲区;
HttpResponse响应;
HttpClient-HttpClient;
列出nameValuePairs;
私有静态字符串pid;
私有静态字符串用户名;
私有静态字符串密码;
//进度对话框
私人对话;
//JSON解析器类
JSONParser JSONParser=新的JSONParser();
//单人网址
// ******************************************************************
//google android emulator中的localhost=10.0.2.2
//genymotion仿真器中的本地主机=10.0.3.2
// ******************************************************************
私有静态最终字符串url\u检查\u登录=”http://10.0.3.2/check.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
私有静态最终字符串标记_PERSON=“PERSON”;
//私有静态最终字符串标记_PID=“PID”;
私有静态字符串标记_NAME=“Username”;
私有静态字符串TAG_pass=“password”;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b=(按钮)findViewById(R.id.Button01);
et=(EditText)findViewById(R.id.username);
pass=(EditText)findViewById(R.id.password);
tv=(TextView)findviewbyd(R.id.tv);
b、 setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//在后台线程中获取完整的人员详细信息
新建CheckLogin().execute();
}
});
}
/**
*获取完整人员详细信息的后台异步任务
* */
类CheckLogin扩展了异步任务{
JSONArray productObj;
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(AndroidPHPConnectionDemo.this);
pDialog.setMessage(“正在加载人员详细信息。请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(真);
pDialog.show();
}
/**
*在后台线程中获取人员详细信息
* */
@凌驾
受保护的字符串doInBackground(字符串…arg0){
//从后台线程更新UI
//检查成功标签
//Username=et.getText().toString();
//Log.e(“创建方法中的用户名”,“用户名为”+
//用户名);
成功;
试一试{
//建筑参数
List params=new ArrayList();
添加(新的BasicNameValuePair(“用户名”,用户名));
//通过发出HTTP请求获取人员详细信息
//请注意,person details url将使用GET请求
JSONObject json=jsonParser.makeHttpRequest(url\u check\u login,
“得到”,params);
//e(“JsonObject”,json.toString());
//检查日志中的json响应
//Log.d(“单人详细信息”,json.toString());
//json成功标记
success=json.getInt(TAG_success);
如果(成功==1){
//已成功接收个人详细信息
productObj=json.getJSONArray(TAG_PERSON);//json数组
//TAG_NAME=et.getText().toString();
//Log.e(“创建方法中的用户名”,“用户名为”+TAG_NAME);
}
否则{
//找不到具有pid的产品
}
}捕获(JSONException e){
e、 printStackTrace();
}
返回null;
}
/**
*完成后台任务后,关闭“进度”对话框
* **/
受保护的void onPostExecute(字符串文件\u url){
//解雇
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
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 AndroidPHPConnectionDemo extends Activity {
Button b;
EditText et, pass;
TextView tv;
HttpPost httppost;
StringBuffer buffer;
HttpResponse response;
HttpClient httpclient;
List<NameValuePair> nameValuePairs;
private static String pid;
private static String Username;
private static String Password;
// Progress Dialog
private ProgressDialog pDialog;
// JSON parser class
JSONParser jsonParser = new JSONParser();
// single person url
// ******************************************************************
// the localhost in the google android emulator = 10.0.2.2
// the localhost in the genymotion emulator = 10.0.3.2
// ******************************************************************
private static final String url_check_login = "http://10.0.3.2/check.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PERSON = "person";
// private static final String TAG_PID = "pid";
private static String TAG_NAME = "Username";
private static String TAG_pass = "password";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b = (Button) findViewById(R.id.Button01);
et = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
tv = (TextView) findViewById(R.id.tv);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// Getting complete person details in background thread
new CheckLogin().execute();
}
});
}
/**
* Background Async Task to Get complete person details
* */
class CheckLogin extends AsyncTask<String, String, String> {
JSONArray productObj;
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(AndroidPHPConnectionDemo.this);
pDialog.setMessage("Loading person details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting person details in background thread
* */
@Override
protected String doInBackground(String... arg0) {
// updating UI from Background Thread
// Check for success tag
// Username = et.getText().toString();
// Log.e("username in create method", " the username is " +
// Username);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", Username));
// getting person details by making HTTP request
// Note that person details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(url_check_login,
"GET", params);
} catch (JSONException e) {
e.printStackTrace();
}
return json.toString();
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String json) {
// dismiss the dialog once got all details
int success;
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received person details
productObj = json.getJSONArray(TAG_PERSON); // JSON Array
// TAG_NAME = et.getText().toString();
// Log.e("username in create method", " the username is " + TAG_NAME);
}
if (productObj != null) {
try {
// get first product object from JSON Array
JSONObject person = productObj.getJSONObject(0);
et.setText(person.getString(Username));
//TAG_NAME = Username;
pass.setText(person.getString(TAG_pass));
Toast.makeText(
getBaseContext(),
et.getText().toString() + pass.getText().toString(),
Toast.LENGTH_LONG).show();
Log.e("success in login", "SUCCESS IN LOGIN");
} catch (Exception e) {
e.printStackTrace();
}
}
Log.e("after the post execute", " THE USERNAME IS " + Username);
pDialog.dismiss();
}
}
}