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);}
}
类 get_Username 是我的异步任务,它没有在我的所有者主 Activity 中运行
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 列上显示用户名的所有者 Activity
<?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 获取服务器文件夹中的用户名_by_userid.php。
目前,我正在开发一个允许用户登录并将 mySQL 中的 user_id 发送到所有者 Activity 的 Android 应用程序,TextView 应该向用户显示在 get_user 的列中查看他或她的用户名。感谢提前。
最佳答案
仅供引用,这与 build.gradle 和 Android Studio 版本无关。
请检查您的 doInBackground() 方法。您在这里返回“空”。您应该在此处返回所需的字符串。在 postExecute() 中捕获结果
protected void onPostExecute(String value)
{
//Update your UI here using 'value'
// super.onPostExecute(file_url);}
}
希望这对您有所帮助。
关于php - 安卓 API 15 : AsyncTask not running (Android Studio 1. 3.2),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32882190/