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

Php Android API 15:AsyncTask未运行(Android Studio 1.3.2),php,android,android-asynctask,Php,Android,Android Asynctask,这是我的android build.gradle apply plugin: 'com.android.application' android { compileSdkVersion 15 buildToolsVersion "23.0.1" defaultConfig { applicationId "com.android.justmakan.androidhive" minSdkVersion 15 targetSdkVersion 15 versio

这是我的android build.gradle

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'}
    }
}
假定为在TextView inputUsername列上显示用户名的所有者活动

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);

使用@Override覆盖您的DoingInBackground顶部并使用

<?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);}

?>
而不是

new get_Username().executeOnExecutor();

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

请检查您的doInBackground()方法。您在此处返回“null”。您应该在此处返回所需的字符串。在postExecute()中捕获结果

}


希望这能有所帮助。

您的变量
username
声明在哪里?@NicholasAllio它在类所有者下,我删除了它,因为建议声明的变量太多,您应该修改自定义AsyncTask并在
onPostExecute
方法中返回值;该方法是唯一能够与运行
所有者
活动的主线程通信的方法。我是否需要在Asynctask上编辑以返回值?它返回null,但变量“username”在所有类(全局)下声明,因此,如果该方法更新了username的值并在所有者classIn中使用,那么您将设置“inputUsername.setText(username);”在获得异步任务结果之前。只需将它放在AsyncTask中的onPostExecute()方法中,这样您就可以检查值了。我在onPostExecute()下对它进行了修改,比如:runOnUiThread(new Runnable(){public void run(){inputUsername.setText(username);inputUsername.setEnabled(false);});并在doingInBackground()末尾检索后返回username。解析数据org.json.JSONException:Value日志时出错,显示该值仍然为null。我的doinbackground()中是否存在任何逻辑错误?分析数据时出错..Pirisok,由于以下错误无法实现:错误:类AsyncTask中的方法executeOnExecutor无法应用于给定类型;必需:执行人,发现无效[]:无争论原因:实际争论列表和正式争论列表长度不同
new get_Username().executeOnExecutor();
new get_Username().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
 protected void onPostExecute(String value)
{
   //Update your UI here using 'value'
  //  super.onPostExecute(file_url);}