2014-09-23 23 views
0

我試圖更新3個接口元素,但它不會,也是我的實現正確嗎?doInBackground和更新界面

元素是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執行是正確的。

+0

你爲什麼在doinBackground()中使用其他線程? asynctask的doinBackground在單獨的線程中是成功的。 – Jithu 2014-09-23 13:15:21

+0

我該怎麼辦? – John 2014-09-23 13:18:04

回答

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更新您的觀看次數。

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