Android API 15:AsyncTask未运行(Android Studio 1.3.2)

时间:2015-10-01 07:50:32

标签: php android android-asynctask

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在服务器文件夹中获取get_username_by_userid.php。

目前,我正在开发一个Android应用程序,允许用户登录并将mySQL中的user_id发送给所有者活动,TextView应该显示用户在get_user列上查看他或她的用户名。感谢提前。

2 个答案:

答案 0 :(得分:1)

仅供参考,这与build.gradle和Android Studio版本无关。

请检查您的doInBackground()方法。你要回来了#null;&#39;这里。你应该在这里返回所需的字符串。在postExecute()

中捕获结果
 protected void onPostExecute(String value)
{
   //Update your UI here using 'value'
  //  super.onPostExecute(file_url);}

}

希望这有帮助。

答案 1 :(得分:0)

使用doObackgroud的@Override顶部并使用

new get_Username().executeOnExecutor();

而不是

new get_Username().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);