編集テキストに入力された URL アドレスのソースを取得する必要があるという要件があります。HTTPGET リクエストでハードコードされた URL のソース ファイルを取得できます。コードは以下の通りです..
public String getHtml() throws ClientProtocolException, IOException
{
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("http://www.google.com");
HttpResponse response = httpClient.execute(httpGet, localContext);
BufferedReader reader = new BufferedReader(
new InputStreamReader(
response.getEntity().getContent()
)
);
String line = null;
while ((line = reader.readLine()) != null){
result += line + "\n";
Log.d("result","="+result);
}
return result;
}
そして、次のようにファイルに保存します。
public void onClick(View v) {
File file = new File(etPath.getText().toString());
FileWriter writer=null;
try
{
writer = new FileWriter(file);
/** Saving the contents to the file*/
writer.write(getHtml());
/** Closing the writer object */
writer.close();
/** Getting sharedpreferences object to store the path of last saved file */
SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit();
/** Setting the last saved file's path in the shared preference */
editor.putString("fpath", file.getPath());
/** Save the path to the shared preference */
editor.commit();
Toast.makeText(getBaseContext(), "Successfully saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
};
このメソッドは google.com のソースを適切に返しています。しかし、私はこのようなことをする必要があります:
HttpGet httpGet = new HttpGet(etContent.getText().toString());
etContent は、URL が入力される編集テキストです。しかし、このステートメントを実行すると、クラッシュします。
編集テキストに入力された URL のソースを抽出する必要があります。非同期タスクを使用するつもりはありません。みんな助けてください。
サポートにいくつかのコードを投稿してください。ありがとう