2012-12-01 140 views
1

我讀的是AsyncTask,我嘗試了一個程序。但它似乎並不奏效。如果可能的話,我是編程新手,請你幫我解決。AsyncTask,doInBackground()不運行

public class MainActivity extends Activity { 

TextView title; 
TextView date; 
TextView cat; 
TextView desc; 
Button rss; 

difficultTask doBack; 

private static final String TAG_ITEM = "item"; 
private static final String TAG_TITLE = "title"; 
private static final String TAG_LINK = "link"; 
private static final String TAG_DESC = "description"; 
private static final String TAG_DATE = "pubDate"; 
private static final String TAG_CAT = "category"; 


JSONArray rssFeed = null; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main);   
    title = (TextView)findViewById(R.id.title);     
    date = (TextView)findViewById(R.id.date);   
    cat = (TextView)findViewById(R.id.cat);   
    desc = (TextView)findViewById(R.id.description);   
    rss = (Button)findViewById(R.id.rss); 
    rss.setOnClickListener(new Button.OnClickListener() {    
     @Override 
     public void onClick(View v) { 
      doBack = new difficultTask(); 
      doBack.execute();     
      } 
     });   
} 

class difficultTask extends AsyncTask<Void, Void, Void>{ 
    @Override 
    protected Void doInBackground(Void... params) {   

     JSONObject json = getJSONFromUrl("http://example.com/mysite/rss.js"); 

     String categ = "";String t = "";String d = "";String da = "";String a = ""; 

     try {  
      JSONArray jsonArray = json.getJSONArray(TAG_ITEM); 
      for(int i = 0; i<jsonArray.length(); i++){ 
       JSONObject c = rssFeed.getJSONObject(i); 
       t = c.getString(TAG_TITLE); 
       a = c.getString(TAG_LINK); 
       d = c.getString(TAG_DESC); 
       da = c.getString(TAG_DATE); 
       categ = c.getString(TAG_CAT); 

      } 

      Log.d("Log", t+"/n"+a+"/n"+d+"/n"+da+"/n"+categ); 
     } catch (JSONException e) { 
      Log.d("Log", e.getMessage()); 
      e.printStackTrace(); 
     }    
     Log.d("Log", "Background"); 
     return t; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     super.onPostExecute(result); 
     title.setText(result); 
    } 

    public JSONObject getJSONFromUrl(String url){ 
     InputStream ips = null; 
     JSONObject jsonObj = null; 
     String json = ""; 

     try{ 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httppost = new HttpPost(url);    
      HttpResponse respone = httpClient.execute(httppost); 
      HttpEntity entity = respone.getEntity(); 

      ips = entity.getContent(); 
     } 
     catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     try{ 
      BufferedReader bufff = new BufferedReader(new InputStreamReader(ips, "UTF-8")); 
      StringBuilder sb = new StringBuilder(); 
      String line = null; 
      while((line = bufff.readLine()) != null){ 
       sb.append(line + "\n"); 
      } 
      ips.close(); 
      json = sb.toString(); 
     } 
     catch(Exception e){ 
      Log.d("Log", e.toString()); 
     } 
      try { 
       jsonObj = new JSONObject(json); 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     return jsonObj;   
    } 


} 

} 

doInBackground()不工作,請幫忙。 而我怎麼能從背景中返回一些String s t,a,c,d,da?

UPD:對不起logcat的沒有工作,現在我看到錯誤的

12-01 19:17:30.109: E/AndroidRuntime(2500): FATAL EXCEPTION: AsyncTask #1 
12-01 19:17:30.109: E/AndroidRuntime(2500): java.lang.RuntimeException: An error occured while executing doInBackground() 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at android.os.AsyncTask$3.done(AsyncTask.java:299) 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352) 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at java.util.concurrent.FutureTask.setException(FutureTask.java:219) 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at java.util.concurrent.FutureTask.run(FutureTask.java:239) 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230) 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080) 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573) 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at java.lang.Thread.run(Thread.java:856) 
12-01 19:17:30.109: E/AndroidRuntime(2500): Caused by: java.lang.NullPointerException 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at com.example.mytwitter.MainActivity$difficultTask.doInBackground(MainActivity.java:99) 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at com.example.mytwitter.MainActivity$difficultTask.doInBackground(MainActivity.java:1) 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at android.os.AsyncTask$2.call(AsyncTask.java:287) 
12-01 19:17:30.109: E/AndroidRuntime(2500):  at java.util.concurrent.FutureTask.run(FutureTask.java:234) 
12-01 19:17:30.109: E/AndroidRuntime(2500):  ... 4 more 
+0

日誌很簡單。閱讀「造成」。你有什麼困難? – rds

+0

記錄清楚地表明99行的NPE。檢查99行是什麼 –

+2

只需在doInBackground中檢查您的代碼即可。也許你沒有收到JSON對象,或者它沒有包含「item」(請看第99行)。你也可以在你初始化的地方嘗試變量「rssFeed」?也許你換成「jsonArray」? – nfirex

回答

0

好吧,我已經添加了一些簡單的Asynctask爲you..try,並告訴我這是否工作給你。

public class SenderActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     SenderActivity.Sender.execute(); 
    } 

    class Sender extends AsyncTask<Void, Integer, Integer> { 
     ProgressDialog pd = null; 
     @Override 
     protected void onPreExecute() { 
      // TODO Auto-generated method stub 
      super.onPreExecute(); 
      pd = new ProgressDialog(SenderActivity.this); 
      pd.setTitle("Sending..."); 
      pd.setMessage("Please wait.."); 
      pd.setCancelable(false); 
      pd.show(); 
     } 
     @Override 
     protected Integer doInBackground(Void... params) { 
      System.out.println("Try some simple operation to learn well"); 
      //check whether this printing in your logcat 
      return 3; 
     } 

     @Override 
     protected void onPostExecute(Integer result) { 
      super.onPostExecute(result); 
      pd.dismiss(); 
     } 
    } 
} 
0
JSONObject json = getJSONFromUrl("http://example.com/mysite/rss.js"); 

AsyncTask是完美的,你應該做一個日誌此語句前檢查什麼getJSONFromUrl()正在恢復,我覺得這個方法返回這就是爲什麼你面對錯誤。

相關問題