2016-06-14 5 views
0

私は、ウェブサイト全体の内容をハッシュコード(ボタンをクリックすると)に割り当て、私は初心者なので行き詰まってしまいました。これまでのところ、私はこれまでのところ得ることができた:ウェブサイトのコンテンツ全体を文字列値にする(アンドロイド)

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を読み込むために得ることができない、バッファリングリーダーです。最後のコメントは私が試したことの1つですが、それはまた私のためにうまくいかなかったのです。問題は、バッファリングされた読者がURLを読み込んで保存しないのはなぜですか?

私は、ハッシュされたウェブサイトの最初のバイトをチェックし、それが偶数の場合はデータベースに、それが奇数の場合はSharedPreferenceに書き込む必要がありますが、後でそれを理解しようとします。

答えて

0

投稿したコードは、a変数にウェブページのコンテンツを保存しています。 inputLineには、in.readLine()によって返された最後の値(null)が含まれます。あなたは

int hashedSite = a.toString().hashCode(); 

int hashedSite = inputLine.hashCode(); 

を変更する必要が

はまた、あなたがUIスレッド上でこのすべてを実行してきたようです。アンドロイドでは、ネットワーク関連のタスクはバックグラウンドスレッドで行う必要があります。あなたは見てくださいAsyncTask

関連する問題