2013-05-13 100 views
0

林建設我的第一個應用程序和即時消息一種新的具體線路於此,而是:HTTP URL連接:顯示的網站源代碼

我使用HttpURLConnection的發送HTTP GET請求。這裏是我的活動代碼:

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 java.net.URLConnection; 
import android.app.Activity; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class HttpGetServletActivity3 extends Activity implements 
    OnClickListener { 
Button button; 
TextView outputText; 

public static final String URL = 
    "URL"; 

/** Called when the activity is first created. */ 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    findViewsById(); 

    button.setOnClickListener(this); 
} 

private void findViewsById() { 
    button = (Button) findViewById(R.id.button); 
    outputText = (TextView) findViewById(R.id.outputTxt); 
} 

public void onClick(View view) { 
    GetXMLTask task = new GetXMLTask(); 
    task.execute(new String[] { URL }); 
} 

private class GetXMLTask extends AsyncTask<String, Void, String> { 
    @Override 
    protected String doInBackground(String... urls) { 
     String output = null; 
     for (String url : urls) { 
      output = getOutputFromUrl(url); 
     } 
     return output; 
    } 

    private String getOutputFromUrl(String url) { 
     StringBuffer output = new StringBuffer(""); 
     try { 
      InputStream stream = getHttpConnection(url); 
      BufferedReader buffer = new BufferedReader(
        new InputStreamReader(stream)); 
      String s = ""; 
      while ((s = buffer.readLine()) != null) 
       output.append(s); 
     } catch (IOException e1) { 
      e1.printStackTrace(); 
     } 
     return output.toString(); 
    } 

    // Makes HttpURLConnection and returns InputStream 
    private InputStream getHttpConnection(String urlString) 
      throws IOException { 
     InputStream stream = null; 
     URL url = new URL(urlString); 
     URLConnection connection = url.openConnection(); 

     try { 
      HttpURLConnection httpConnection = (HttpURLConnection) connection; 
      httpConnection.setRequestMethod("GET"); 
      httpConnection.connect(); 

      if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) { 
       stream = httpConnection.getInputStream(); 
      } 
     } catch (Exception ex) { 
      ex.printStackTrace(); 
     } 
     return stream; 
    } 

    @Override 
    protected void onPostExecute(String output) { 
     outputText.setText(output); 
    } 
} 
} 

此代碼將幫助我顯示一個網站的源代碼,它的工作! 但是如果我想要特定的源代碼行?僅示例60行? 我試圖更改以下BuuferedReader塊:

BufferedReader buffer = new BufferedReader(
        new InputStreamReader(stream)); 
      String s = ""; 
      while ((s = buffer.readLine()) != null) 
       output.append(s); 

而且我寫了下面LineReader代替,但沒有工作:

LineNumberReader lnr = new LineNumberReader(new InputStreamReader(stream)); 
      String s = ""; 
      if (lnr.getLineNumber() == 60){ 
       output.append(s); 
      } 

這是做還是我失去了什麼呢?

回答

0

您可以使用setLineNumber方法;

lnr.setLineNumber(60) 

然後,您可以閱讀該行!

0

如果你試圖讓您知道邊界之間的代碼的一部分,那麼你可以這樣做(即和。):

轉換流,以一個String(我們會打電話給它的結果),然後:

String start = "<body>", end = "</body>"; 
result = result.substring(result.indexOf(start) + start.length(), result.indexOf(end));