2013-10-21 8 views
0

我想從Web服務獲取XML,我將它存儲在字符串中。我需要在Log貓中打印這個xml,但它沒有以正確的方式進入。 代碼是:爲什麼我在String中收集的xml輸出沒有以正確格式顯示?

MainActivity.java

public class MainActivity extends Activity { 
    static final String URL = myUrlOfLocalHost; 

    // XML node keys 
    static final String KEY_ITEM = "item"; // parent node 
    static final String KEY_ID = "id"; 
    static final String KEY_NAME = "name"; 
    static final String KEY_COST = "cost"; 
    static final String KEY_DESC = "description"; 
    private String xml; 

    @SuppressLint("NewApi") 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     AsyncTaskRunner runner = new AsyncTaskRunner(); 
     runner.execute(); 

    } 

    class AsyncTaskRunner extends AsyncTask<String, String, String> { 

     @SuppressLint("NewApi") 
     @Override 
     protected String doInBackground(String... params) { 

      publishProgress("Sleeping..."); // Calls onProgressUpdate() 
      ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>(); 

      XMLParser parser = new XMLParser(); 

      xml = parser.getXmlFromUrl(URL); // getting XML 
      //Document doc = parser.getDomElement(xml); // getting DOM element 



      Log.e("String Data : " , xml); 
return null; 
} 

XMLParser.java

public class XMLParser { 


    private StringBuffer result ; 

    //Method to get XML from reuested URL 
    public String getXmlFromUrl(String url) { 

     try { 
      // defaultHttpClient 
      DefaultHttpClient httpClient = new DefaultHttpClient(); 
      HttpPost httpPost = new HttpPost(url); 

      HttpResponse httpResponse = httpClient.execute(httpPost); 
      HttpEntity httpEntity = httpResponse.getEntity(); 
      //xml = EntityUtils.toString(httpEntity); 

      InputStream in = httpEntity.getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(in, "UTF-8"),8); 
      StringBuffer sb = new StringBuffer(); 
      String line = null; 
      while ((line = reader.readLine()) != null) 
      { 
       // sb.append(line); 
       result = sb.append(line);; 
      } 

     } catch (UnsupportedEncodingException e) { 
      e.printStackTrace(); 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     // return XML complete 
     return result.toString(); 
    } 

請建議任何解決方案,在logcat的輸出來了象下面這樣:

<pre class="cake-error"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5265202999359-trace').style.display = (document.getElementById('cakeErr5265202999359-trace').style.display == 'none' ? '' : 'none');"><b>Notice</b> (8)</a>: Undefined variable: school [<b>APP\View\Schools\xml\get_school_details.ctp</b>, line <b>1</b>]<div id="cakeErr5265202999359-trace" class="cake-stack-trace" style="display: none;"><a href="javascript:void(0);" onclick="document.getElementById('cakeErr5265202999359-code').style.display = (document.getElementById('cakeErr5265202999359-code').style.display == 'none' ? '' : 'none')">Code</a> <a href="javascript:void(0);" onclick="document.getElementById('cakeErr5265202999359-context').style.display = (document.getElementById('cakeErr5265202999359-context').style.display == 'none' ? '' : 'none')">Context</a><pre id="cakeErr5265202999359-code" class="cake-code-dump" style="display: none;"><span class="code-highlight"><code><span style="color: #000000"><span style="color: #0000BB">&lt;?php&nbsp;$xml&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #0000BB">Xml</span><span style="color: #007700">::</span><span style="color: #0000BB">fromArray</span><span style="color: #007700">(array(</span><span style="color: #0000BB">$school</span><span style="color: #007700">));</span></span></code></span></pre><pre id="cakeErr5265202999359-context" class="cake-context" style="display: none;">$viewFile = &#039;C:\wamp\www\School App\app\View\Schools\xml\get_school_details.ctp&#039;$dataForView = array()</pre><pre class="stack-trace">include - APP\View\Schools\xml\get_school_details.ctp, line 1View::_evaluate() - CORE\Cake\View\View.php, line 931View::_render() - CORE\Cake\View\View.php, line 893View::render() - CORE\Cake\View\View.php, line 462XmlView::render() - CORE\Cake\View\XmlView.php, line 104Controller::render() - CORE\Cake\Controller\Controller.php, line 952Dispatcher::_invoke() - CORE\Cake\Routing\Dispatcher.php, line 194Dispatcher::dispatch() - CORE\Cake\Routing\Dispatcher.php, line 162[main] - APP\webroot\index.php, line 110</pre></div></pre><?xml version="1.0" encoding="UTF-8"?><response> <code>500</code> <url>/School%20App/schools/getSchoolDetails.xml?school_id=1</url> <name>The key of input must be alphanumeric</name></response> 

在使用服務器端方法(代碼)是:

public function getSchoolDetails() { 
    if ($this->RequestHandler->isXml() && $this->request->is('get')) { 
     $this->School->unBindModel(array('hasMany' => array('Admin', 'Announcement', 'Batch', 'Class1', 'Event', 'Lunchmenu', 'Student', 'Timetable', 'SpecialInstruction'),           'hasAndBelongsToMany' => array('Subject'))); 
     $fields = array('School.id, School.school_name, School.logo, School.phone'); 
     $school = $this->School->find('first', array('fields' => $fields, 'conditions' => array('School.id' => $_GET['school_id']))); 
     $this->set(array(
      'school' => $school, 
     '_serialize' => array('school') 
     )); 
    } 
} 
+0

從web service返回的xml中存在一些xml問題。請將該URL粘貼到Web瀏覽器中,並檢查它是否正確? – Subburaj

+0

是的,先生..我檢查了這一點。在瀏覽器中,xml以正確的方式進入。 – user2890202

+0

請始終提及您使用的確切CakePHP版本! – ndm

回答

0

如果這是logcat的那麼它看起來像在serversize一個PHP的問題...

....CORE\Cake\Routing\Dispatcher.php.... 
+0

好的先生..謝謝你! – user2890202

+0

Web服務正在瀏覽器中工作。但是,儘管在Android的代碼集,它是上面提到的給予在堆棧跟蹤數據..請指導我,先生,如果你知道任何解決方案...謝謝 – user2890202

+0

我仍然有一個關於服務器端問題的懷疑:PHP似乎輸出錯誤到流以及XML。跟蹤底部有一段XML:'<?xml version =「1.0」encoding =「UTF-8」?>500 /School%20App/schools/getSchoolDetails.xml?school_id=1輸入的關鍵字必須是字母數字'。我假設這是需要解析的位? – 2013-10-21 13:49:08

0

您使用的是POST請求,而你的控制器動作只能用GET要求:

if ($this->RequestHandler->isXml() && $this->request->is('get')) { 

這將導致不能在非XML/POST爲r的情況下設置的變量equests,導致您收到的錯誤。

速戰速決,讓POST請求過或使用GET請求代替。另外我建議改變你的控制器動作,以便在請求不符合要求時觸發適當的異常。下面是一個(未經測試的)示例:

public function getSchoolDetails() { 
    // NOTE you don't necessarily need this, you could also force the type 
    // of response using $this->RequestHandler->renderAs($this, 'xml'); 
    if (!$this->RequestHandler->isXml()) { 
     throw new BadRequestException(); 
    } 

    // NOTE as of CakePHP 2.3 you could use $this->request->onlyAllow('get'); 
    // instead 
    if (!$this->request->is('get')) { 
     throw new MethodNotAllowedException(); 
    } 

    // NOTE use the request object instead of $_GET, it will safely return 
    // null on non-existent parameters 
    $id = $this->request->query('school_id'); 

    // NOTE skip this in case you want to retrieve empty results instead 
    if (!$this->School->exists($id)) { 
     throw new NotFoundException(); 
    } 

    // NOTE you should use the containable behavior instead of this 
    // unbinding orgy 
    $this->School->unbindModel(array(
     'hasMany' => array(
      'Admin', 'Announcement', 'Batch', 'Class1', 
      'Event', 'Lunchmenu', 'Student', 'Timetable', 
      'SpecialInstruction' 
     ), 
     'hasAndBelongsToMany' => array(
      'Subject' 
     ) 
    )); 

    $fields = array(
     'School.id, School.school_name, School.logo, School.phone' 
    ); 
    $school = $this->School->find('first', array(
     'fields' => $fields, 
     'conditions' => array(
      'School.id' => $id 
     ) 
    )); 

    $this->set(array(
     'school' => $school, 
     '_serialize' => array('school') 
    )); 
} 
相關問題