【发布时间】:2015-12-29 04:32:57
【问题描述】:
apply plugin: 'com.android.application'
android {
compileSdkVersion 15
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.android.justmakan.androidhive"
minSdkVersion 15
targetSdkVersion 15
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'}
}
}
这是我的 android build.gradle。
class get_Username extends AsyncTask<Void, Void, Void> {
protected void onPreExecute()
{
super.onPreExecute();
}
protected Void doInBackground(Void... args) {
params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("user_id", user_id));
json = jParser.makeHttpRequest(url_get_username, "POST", params);
Log.e("User Response: ", "> " + json);
try {
int success = json.getInt("success");
if (success == 1) {
JSONArray usernames = json.getJSONArray("users");
for(int g = 0; g < usernames.length(); g++)
{
JSONObject o = usernames.getJSONObject(g);
username = o.getString("username");
}
}
} catch (JSONException e) {
e.printStackTrace();}
return null;
}
protected void onPostExecute(Void file_url)
{
super.onPostExecute(file_url);}
}
class get_Username 是我的 asynctask,它没有在我的所有者主要活动中运行
public class Owner extends Activity implements AdapterView.OnItemSelectedListener {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.owner_activity);
spinnerLocation = (Spinner) findViewById(R.id.spinLocation);
spinnerFood = (Spinner) findViewById(R.id.spinFood);
inputUsername = (EditText) findViewById(R.id.etUsername);
inputDescription = (EditText) findViewById(R.id.etDescription);
// spinnerLocation.setOnItemSelectedListener(this);
// getting product details from intent
Intent tempI = getIntent();
user_id = tempI.getStringExtra("user_id");
new get_Username().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
inputUsername.setText(username);
inputUsername.setEnabled(false);
假设在 TextView inputUsername 列上显示用户名的所有者活动
<?php
$response = array();
if (isset($_POST['user_id']))
{
$user_id = $_POST['user_id'];
require_once 'db_connect.php';
$db = new DB_CONNECT();
$results = mysql_query("SELECT * FROM user WHERE user_id = '$user_id'") or die(mysql_error());
$response["users"] = array();
while ($row = mysql_fetch_array($results))
{
$users = array();
$users["username"] = $row["username"]
array_push($response["users"], $users);
}
// success
$response["success"] = 1;
// echoing json result
echo json_encode($response);}
else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
echo json_encode($response);}
?>
使用JsonParser通过android获取服务器文件夹中的_username_by_userid.php。
目前,我正在开发一个允许用户登录并将 mySQL 中的 user_id 发送到所有者活动的 android 应用程序,TextView 应该显示用户从 get_user 的列中看到他或她的用户名。感谢提前。
【问题讨论】:
-
你的变量
username在哪里声明? -
@NicholasAllio 它在 Owner 类下,由于声明的变量太多,我将其删除
-
正如DGN 建议的那样,您应该修改自定义AsyncTask 并在
onPostExecute方法中返回您的值;这是唯一能够与您的OwnerActivity 正在运行的主线程进行通信的方法。
标签: php android android-asynctask