doInBackground和更新接口

时间:2014-09-23 13:08:36

标签: java android interface

我试图更新3个接口元素,但它不会,我的实现也正确吗?

元素是txtName,txtClarety,txtDesc。 我不知道为什么......我无法理解这个问题......

        protected String doInBackground(String... params) {

        // updating UI from Background Thread
         Thread th = new Thread(new Runnable() {
            public void run() {
                // Check for success tag
                int success;
                try {
                    // Building Parameters
                    List<NameValuePair> params = new ArrayList<NameValuePair>();
                    params.add(new BasicNameValuePair("pid", pid));

                    // getting dream details by making HTTP request
                    // Note that dream details url will use GET request
                    JSONObject json = jsonParser.makeHttpRequest(
                            url_dream_detials, "GET", params);

                    // check your log for json response
                    Log.d("Single Dream Details", json.toString());

                    // json success tag
                    success = json.getInt(TAG_SUCCESS);
                    if (success == 1) {
                        // successfully received dream details
                        JSONArray dreamObj = json.getJSONArray(TAG_DREAM); // JSON Array

                        // get first dream object from JSON Array
                        JSONObject dream = dreamObj.getJSONObject(0);

                        // dream with this pid found
                        // Edit Text
                        txtName = (EditText) findViewById(R.id.inputEditName);
                        txtClarety = (EditText) findViewById(R.id.inputEditClarety);
                        txtDesc = (EditText) findViewById(R.id.inputEditDesc);

                        // display dream data in EditText
                        txtName.setText(dream.getString(TAG_NAME));
                        txtClarety.setText(dream.getString(TAG_CLARETY));
                        txtDesc.setText(dream.getString(TAG_DESCRIPTION));


                    }else{
                        // dream with pid not found
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        });

        return null;

    }

我尝试过重命名我的输入,不同的名字,但没有...

JSON执行是正确的。

4 个答案:

答案 0 :(得分:0)

doInBackground已经使用了另一个帖子。因此,您不需要创建自己的Thread实例 您还应该更新onPostExecute中的观看次数。

请参阅文档:http://developer.android.com/reference/android/os/AsyncTask.html 你需要知道的一切都在那里解释。

答案 1 :(得分:0)

   class async extends AsyncTask<String, Void, JSONArray>
    {

        @Override
        protected JSONArray doInBackground(String... params) {
            JSONArray dreamObj;
            try {
                // Building Parameters
                List<NameValuePair> param = new ArrayList<NameValuePair>();
                param.add(new BasicNameValuePair("pid", pid));

                // getting dream details by making HTTP request
                // Note that dream details url will use GET request
                JSONObject json = jsonParser.makeHttpRequest(
                        url_dream_detials, "GET", param);

                // check your log for json response
                Log.d("Single Dream Details", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    // successfully received dream details
                    dreamObj = json.getJSONArray(TAG_DREAM); // JSON Array
                    return dreamObj;

                }else{
                    // dream with pid not found
                    return dreamObj;
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return dreamObj;
        }
        @Override
        protected void onPostExecute(JSONArray result) {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            // get first dream object from JSON Array
            JSONObject dream = result.getJSONObject(0);

            // dream with this pid found
            // Edit Text
            txtName = (EditText) findViewById(R.id.inputEditName);
            txtClarety = (EditText) findViewById(R.id.inputEditClarety);
            txtDesc = (EditText) findViewById(R.id.inputEditDesc);

            // display dream data in EditText
            txtName.setText(dream.getString(TAG_NAME));
            txtClarety.setText(dream.getString(TAG_CLARETY));
            txtDesc.setText(dream.getString(TAG_DESCRIPTION));
        }
    }

试试这个。您不能在doInBackground中更新您的视图。

答案 2 :(得分:0)

class SampleAsynctask extends AsyncTask<String, Void, String>
    {

    @Override
    protected JSONArray doInBackground(String... params) {
        List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("pid", pid));

                // getting dream details by making HTTP request
                // Note that dream details url will use GET request
               try {
                JSONObject json = jsonParser.makeHttpRequest(
                        url_dream_detials, "GET", params);

                // check your log for json response
                Log.d("Single Dream Details", json.toString());

                // json success tag
                success = json.getInt(TAG_SUCCESS);
                if (success == 1) {
                    // successfully received dream details
                    JSONArray dreamObj = json.getJSONArray(TAG_DREAM); // JSON Array

                    // get first dream object from JSON Array
                    JSONObject dream = dreamObj.getJSONObject(0);
        return dreamObj.toString();
     }
    } catch (JSONException e) {
    }
    return "";
    }
    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
     if(result.length() > 0) {
      try {

        JSONObject dream = new JSONObject(result);
      EditText txtName = (EditText) findViewById(R.id.inputEditName);
                   EditText txtClarety = (EditText) findViewById(R.id.inputEditClarety);
                   EditText txtDesc = (EditText) findViewById(R.id.inputEditDesc);

                    // display dream data in EditText
                    txtName.setText(dream.getString(TAG_NAME));
                    txtClarety.setText(dream.getString(TAG_CLARETY));
                    txtDesc.setText(dream.getString(TAG_DESCRIPTION));
     } catch (JSONException e) {
    }
    }
   }
}

答案 3 :(得分:0)

如果您想在Widgets方法的doInBackground()上执行某些操作,请使用runOnUiThread()

例如:在您的doInBackground中,您应该将您的小部件部分代码替换为以下内容:

runOnUiThread(new Runnable() 
        {
            @Override
            public void run() 
            {
                if (success == 1) {
                    // successfully received dream details
                    JSONArray dreamObj = json.getJSONArray(TAG_DREAM); // JSON Array

                    // get first dream object from JSON Array
                    JSONObject dream = dreamObj.getJSONObject(0);

                    // dream with this pid found
                    // Edit Text
                    txtName = (EditText) findViewById(R.id.inputEditName);
                    txtClarety = (EditText) findViewById(R.id.inputEditClarety);
                    txtDesc = (EditText) findViewById(R.id.inputEditDesc);

                    // display dream data in EditText
                    txtName.setText(dream.getString(TAG_NAME));
                    txtClarety.setText(dream.getString(TAG_CLARETY));
                    txtDesc.setText(dream.getString(TAG_DESCRIPTION));


                }else{
                    // dream with pid not found
                }
            }
        });
    }