我有这个代码,使用Asynctask和json在android和php之间创建连接以获取响应,但问题是响应总是为空或为空
我知道它是什么问题: 字符串TAG_NAME 女巫是在php文件中必须相同的JSON节点。
此字符串始终提供“用户名”,不接受用户输入。
所以要解决这个错误? 有人可以帮帮我吗?我会很感激的。
package pack.coderzheaven;
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();
}
}
}
答案 0 :(得分:0)
试试这个并观察代码中的变化,现在就告诉我发生了什么。
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();
}
}
}