2012-03-22 27 views
1

我想在我的android應用程序中使用外部網站http://www.siirretytnumerot.fi/。這個網站有兩個值PREFIX和NUMBER。目前我很困惑,因爲我似乎沒有在我的textview中獲得任何輸出。我不知道要使用httpget還是httppost。我嘗試了兩個,但仍然沒有結果。但是,當我去到資源管理器的鏈接,並輸入一個輸入的網站行更改爲http://www.siirretytnumerot.fi/QueryServlet。我試圖使用兩個仍然沒有輸出。 請有人幫我看看網站,並建議哪些http方法對我來說是正確的? 這裏是我使用的代碼。如何從網站使用Http獲取結果android

TextView tv=(TextView)findViewById(R.id.display); 
    try { 
    HttpClient client = new DefaultHttpClient(); 
    String postURL = "http://www.siirretytnumerot.fi/"; 
    HttpPost post = new HttpPost(postURL); 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("PREFIX", "044")); 
     params.add(new BasicNameValuePair("NUMBER", "9782231")); 
     params.add(new BasicNameValuePair("LANGUAGE", "Finnish")); 
     params.add(new BasicNameValuePair("Submit", "Hae")); 
     UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8); 
     post.setEntity(ent); 
     HttpResponse responsePOST = client.execute(post); 
     HttpEntity resEntity = responsePOST.getEntity(); 
     if (resEntity != null) {  
      tv.setText(EntityUtils.toString(resEntity)); 
     } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 

從鏈路輸出出來作爲圖象源

<img src="QueryServlet?ID=-7187780920186056107&amp;STRING=5WQAy%2BQCUZRGIUJ8qZtpSrmkiKzWp8HRL7Ti1xmFSxMAEZE7GHEtaylOApMGd9qoesY7Pl%2BUN1Z6Kzap9RIg%2Bw==" /> 

現在我該怎樣閱讀?

回答

1

使用瀏覽器的開發工具來找出需要哪些字段。

你應該POST請求http://www.siirretytnumerot.fi/QueryServlet

嘗試將這些字段添加到您的要求:

​​

我希望這會幫助

編輯

它將看起來像那樣

TextView tv=(TextView)findViewById(R.id.display); 
    try { 
    HttpClient client = new DefaultHttpClient(); 
    String postURL = "http://www.siirretytnumerot.fi/"; 
    HttpPost post = new HttpPost(postURL); 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     params.add(new BasicNameValuePair("PREFIX", "044")); 
     params.add(new BasicNameValuePair("NUMBER", "9782231")); 

     //here the new lines 
     params.add(new BasicNameValuePair("LANGUAGE", "Finnish")); 
     params.add(new BasicNameValuePair("Submit", "Hae")); 

     UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params,HTTP.UTF_8); 
     post.setEntity(ent); 
     HttpResponse responsePOST = client.execute(post); 
     HttpEntity resEntity = responsePOST.getEntity(); 
     if (resEntity != null) {  
      tv.setText(EntityUtils.toString(resEntity)); 
     } 
} catch (Exception e) { 
    e.printStackTrace(); 
} 
+0

感謝您迴應,但你能不能編輯代碼並添加你的代碼,以便我能理解你想要我做什麼。 – 2012-03-22 14:41:29

+0

@net_whiz,我添加了代碼 – 2012-03-22 14:46:14

+0

我編輯的代碼仍然沒有在我的textview輸出。什麼可能是錯誤的? – 2012-03-22 14:49:15

1

您可以查看源和有一行

<input type="hidden" name="LANGUAGE" value="Finnish"> 

所以你需要添加字段的,因爲它們可能會使用it.So

params.add(new BasicNameValuePair("LANGUAGE", "Finnish")); 
+0

謝謝,但我編輯了代碼仍然沒有輸出在我的textview。什麼可能是錯誤的? – 2012-03-22 14:49:57

+0

我可能會先將resEntity作爲文本加入到textview中,然後纔開始。是否有任何NetworkOnMainThread異常? – Sankalp 2012-03-22 14:59:31

+0

它顯示,但是得到的是源代碼,但是當我輸入數據後檢查網站輸出的形式是img src: 2012-03-22 15:06:58