0
我有一个哈希代码(单击按钮时)整个网站的内容,我已经打到了一个死胡同(因为我是一个初学者)。到目前为止,我设法远远得到这个:把整个网站内容转换为字符串值(android)
public class MainActivity extends Activity {
Button btn;
EditText urlInput;
TextView urlTxt, hashValue, saveLoc, tv4;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
urlInput = (EditText) findViewById(R.id.urlInput);
urlTxt = (TextView) findViewById(R.id.urlTxt);
hashValue = (TextView) findViewById(R.id.hashValue);
saveLoc = (TextView) findViewById(R.id.saveLoc);
tv4 = (TextView) findViewById(R.id.tv4);
}
public void btnClick(View v) throws IOException, NoSuchAlgorithmException {
Button btn = (Button) v;
urlTxt.setText("Url entered: " + urlInput.getText());
String urlCopy = urlInput.getText().toString();
//problem area ahead ->
URL uri = new URL(urlCopy);
URLConnection ec = uri.openConnection();
BufferedReader in = new BufferedReader(new InputStreamReader(ec.getInputStream(), "UTF-8"));
String inputLine;
StringBuilder a = new StringBuilder();
while ((inputLine = in.readLine()) != null)
a.append(inputLine);
in.close();
int hashedSite = inputLine.hashCode();
hashValue.setText("Hash Value: " + hashedSite);
BigInteger bi = BigInteger.valueOf(hashedSite);
byte[] bytes = bi.toByteArray();
if ((bytes[0] % 2) == 0) {
tv4.setText("First byte is an even number: " + bytes[0]);
} else {
tv4.setText("First byte is and odd number: " + bytes[0]);
}
// String out = new Scanner(new URL(urlCopy).openStream(), "UTF-8").useDelimiter("\\Z").next();
}
}
在那里我坚持的一点是缓冲的读者,我不能让它来读取编辑文本字段该网页的网址。最后的评论是我尝试过的一件事,但它也没有为我工作。问题是为什么缓冲读取器不读取并保存URL?
该任务还要求我检查哈希网站的第一个字节,并将其写入数据库(如果它是偶数)或SharedPreference(如果它是奇数),但我会尽力在后面找出它。