2011-07-27 15 views
0

私はこのコードを持っている:2番目のHTTPClientが動作しないのはなぜですか?

protected void onHandleIntent(Intent intent) { 
     while (true){ 

      long endTime = System.currentTimeMillis() + 5*1000; 
      while (System.currentTimeMillis() < endTime) { 
       synchronized (this) { 
        try { 
         wait(endTime - System.currentTimeMillis()); 


        HttpClient httpclient = new DefaultHttpClient(); 
        HttpPost httppost = new HttpPost("http://www.***.***/***/request_sms.php"); 
        String HTML = ""; 
        try { 
         List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
         nameValuePairs.add(new BasicNameValuePair("id", "1")); 
         httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

         HttpResponse response = httpclient.execute(httppost); 
         HTML = EntityUtils.toString(response.getEntity()); 
        } catch (ClientProtocolException e) {} catch (IOException e) {} 


         if(HTML.indexOf("[NO TEXTS]") > 0) { 

         } else { 
          Vector<String> all_sms = getBetweenAll(HTML, "<sms>", "<sms>"); 
          for(int i = 0, size = all_sms.size(); i < size; i++) { 
           String from = getBetween(all_sms.get(i), "<from>", "</from>"); 
           String to = getBetween(all_sms.get(i), "<to>", "</to>"); 
           String msg = getBetween(all_sms.get(i), "<msg>", "</msg>"); 
           String sent = getBetween(all_sms.get(i), "<sent>", "</sent>"); 
           String HTML1 = ""; 
           HttpClient httpclient1 = new DefaultHttpClient(); 
           HttpPost httppost1 = new HttpPost("http://www.***.***/***/add_sms.php"); 
           try { 
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2); 
            nameValuePairs.add(new BasicNameValuePair("from", from)); 
            nameValuePairs.add(new BasicNameValuePair("to", to)); 
            nameValuePairs.add(new BasicNameValuePair("msg", msg)); 
            nameValuePairs.add(new BasicNameValuePair("sent", sent)); 
            httppost1.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 

            HttpResponse response1 = httpclient1.execute(httppost1); 
            HTML1 = EntityUtils.toString(response1.getEntity()); 
            HN.post(new DisplayToast(HTML1)); 
           } catch (ClientProtocolException e) {} catch (IOException e) {} 

          } 
         } 


         } catch (Exception e) { 
        } 
       } 
      } 

     } 


    } 

これはサービスです、私はそれがやりたいことは、5秒ごとに電話を送信する必要がある保留中のSMSメッセージを持っているページを要求します。私は送信側ではない、私はちょうどHN.Post(DisplayToast(HTML1))が表示され、私は動作します。 HTML1に含めるものは「成功」ですが、何も得られません。私はテストしたようにHTMLに "[NO TEXTS]"が含まれていないことを確信しています。何が間違っていますか?

Handler HN = new Handler(); 


    private class DisplayToast implements Runnable { 

    String TM = ""; 

     public DisplayToast(String toast){ 
      TM = toast; 
     } 

     public void run(){ 
      Toast.makeText(getApplicationContext(), TM, Toast.LENGTH_SHORT).show(); 
     } 
    } 

    public String getBetween(String source, String start, String end) { 

     int startindex = source.indexOf(start); 
     int endindex = source.indexOf(end, startindex); 

     String result = source.substring(startindex + start.length(), endindex); 

     return result; 
    } 

    public Vector<String> getBetweenAll(String source, String start, String end) { 
     int startI = 0; 
     Vector<String> result = new Vector<String>(); 
     while (startI + (start.length() + end.length()) < source.length()) { 
      int startindex = source.indexOf(start, startI); 
      if (startI > startindex) { 
       break; 
      } 
      int endindex = source.indexOf(end, startindex); 
      result.add(source.substring(startindex + start.length(), endindex)); 
      startI = endindex; 
     } 
     return result; 
    } 
+0

エラーが発生しますか?また、5秒ごとに起動されるTimerTaskの使用を検討する必要があります。 –

答えて

0

org.apache.http.impl.client.BasicResponseHandlerを使用してHTMLコンテンツを取得します。

HTML1 = httpclient1.execute(httppost1, new BasicResponseHandler()); 
HN.post(new DisplayToast(HTML1)); 
関連する問題