我正在构建我的第一个应用程序,并且我对此有点新的但是:
我正在使用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 :(得分:0)
您可以使用setLineNumber方法;
lnr.setLineNumber(60)
然后,你可以阅读这行!
答案 1 :(得分:0)
如果你试图让你知道的部分代码在边界之间(即。和),那么你可以做类似的事情:
将Stream转换为单个String(我们将其称为结果),然后:
String start = "<body>", end = "</body>";
result = result.substring(result.indexOf(start) + start.length(), result.indexOf(end));