2012-11-01 44 views
0

我正在研究一個小型應用程序,我試圖從網站讀取一些文本,將文本存儲在變量中,然後在另一個活動中顯示該文本。在我的主要活動中,我有一個名爲「許可協議」的可點擊標籤,點擊後,應用程序應從網站中讀取一些文本,轉到另一個活動,然後爲用戶顯示該文本。但是,每次點擊我的標籤時,我的應用程序部隊都會關閉。有什麼建議麼?在Android應用程序中從URL中讀取文本時遇到困難

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.net.HttpURLConnection; 
import java.net.URL; 


import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 

public class MainActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_main, menu); 
     return true; 
    } 

    public void activity_license(View view) throws IOException{ 


     String stringBuffer; 
     String text =""; 

     URL url = new URL("http://something.something.com/LICENSE"); 

     BufferedReader bufferReader = new BufferedReader(new InputStreamReader(url.openStream())); 

     while((stringBuffer = bufferReader.readLine()) != null){ 
      text += stringBuffer; 
     } 

     bufferReader.close(); 

     Intent licenseIntent = new Intent(this, LicenseActivity.class); 
     licenseIntent.putExtra("LICENSE_AGREEMENT", text); 
     startActivity(licenseIntent); 
    } 
} 

這裏是我的第二個活動代碼,在這裏我想顯示的文字...

import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.view.Menu; 
import android.widget.TextView; 

public class LicenseActivity extends Activity { 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_license); 

     Intent intent = getIntent(); 

     String licenseText = intent.getStringExtra("LICENSE_AGREEMENT"); 

     TextView lblText = (TextView)findViewById(R.id.lblLicense); 
     lblText.setText(licenseText); 
    } 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     getMenuInflater().inflate(R.menu.activity_license, menu); 
     return true; 
    } 
} 
+0

請粘貼logcat的。 – Nikhil

+0

這裏是我的logcat,http://pastebin.com/zcvEVZZB – Massa

回答

0
// RETRIVE YOUR RESPONSE 
    try 
    { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while ((line = reader.readLine()) != null) 
     { 
      sb.append(line + "\n"); 
     } 
     is.close(); 
     result = sb.toString(); 
    } 
    catch (Exception e) 
    { 
     Log.e("Loading Runnable Error converting result :", e.toString()); 
    } 

現在打印此結果作爲字符串。

+0

我剛剛檢查了我的清單,沒關係。 LicenseActivity就在那裏。我也把我的互聯網許可和一切:(... – Massa

+0

使用'這'不是問題,我相信:P。 – Massa

0

試試這個

DefaultHttpClient httpClient = new DefaultHttpClient(); 
HttpPost httpPost = new HttpPost(URL); 

HttpResponse httpResponse = httpClient.execute(httpPost); 
HttpEntity httpEntity = httpResponse.getEntity(); 

InputStream is = httpEntity.getContent(); 
StringBuilder text = new StringBuilder(); 
BufferedReader br = new BufferedReader(new InputStreamReader(is)); 
String line; 
while ((line = br.readLine()) != null) { 
    text.append(line + "\n"); 
} 

try 
    intent.putExtra("licence",text.toString); 

如果這不工作嘗試HTTPGET到位HttpPost的......

+0

仍然得到同樣的力量關閉,即使這個代碼。 – Massa

+0

您正在測試?在設備中還是在模擬器中? – Thirupathig

相關問題