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。Android API 15:AsyncTask未運行(Android Studio 1.3.2)
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是我的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);}
?>
get_username_by_userid的用戶名。通過使用JsonParser通過android的服務器文件夾中的PHP。
目前,我正在開發一個android應用程序,允許用戶登錄並將mySQL中的user_id發送給所有者活動,TextView應該顯示用戶在get_user列中查看他或她的用戶名。感謝提前。
您doingInbackgroud的
你的變量'username'聲明在哪裏? –
@NicholasAllio它是在類的所有者,我刪除它,因爲有太多的變量聲明 – Louis
由於[DGN](http://stackoverflow.com/a/32882490/2064585)提出,你應該修改自定義的AsyncTask並返回你在'onPostExecute'方法中的值;那是唯一能夠與主''活動正在運行的主線程通信的方法。 –