@ZeroGeek
2015-10-27T03:01:35.000000Z
字数 3101
阅读 851
未分类
if (savedInstanceState == null) {FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();RecyclerViewFragment fragment = new RecyclerViewFragment();transaction.replace(R.id.sample_content_fragment, fragment);transaction.commit();}
@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// 。。。return inflater.inflate(R.layout.fragment_notification_builder, container, false);}
@Overridepublic void onViewCreated(View view, Bundle savedInstanceState) {super.onViewCreated(view, savedInstanceState);mNotificationManager = (NotificationManager) getActivity().getSystemService(Context.NOTIFICATION_SERVICE);mNumberOfNotifications = (TextView) view.findViewById(R.id.number_of_notifications);// Supply actions to the button that is displayed on screen.View.OnClickListener onClickListener = new View.OnClickListener() {};view.findViewById(R.id.add_notification).setOnClickListener(onClickListener);}
得到流
private InputStream downloadUrl(String urlString) throws IOException {// BEGIN_INCLUDE(get_inputstream)URL url = new URL(urlString);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setReadTimeout(10000 /* milliseconds */);conn.setConnectTimeout(15000 /* milliseconds */);conn.setRequestMethod("GET");conn.setDoInput(true);// Start the queryconn.connect();InputStream stream = conn.getInputStream();return stream;// END_INCLUDE(get_inputstream)}
流转化为字符串
private String readIt(InputStream stream, int len) throws IOException, UnsupportedEncodingException {Reader reader = null;reader = new InputStreamReader(stream, "UTF-8");char[] buffer = new char[len];reader.read(buffer);return new String(buffer);}
封装操作
/** Initiates the fetch operation. */private String loadFromNetwork(String urlString) throws IOException {InputStream stream = null;String str ="";try {stream = downloadUrl(urlString);str = readIt(stream, 500);} finally {if (stream != null) {stream.close();}}return str;}
通过AysncTask执行
private class DownloadTask extends AsyncTask<String, Void, String> {@Overrideprotected String doInBackground(String... urls) {try {return loadFromNetwork(urls[0]);} catch (IOException e) {return getString(R.string.connection_error);}}/*** Uses the logging framework to display the output of the fetch* operation in the log fragment.*/@Overrideprotected void onPostExecute(String result) {Log.i(TAG, result);}}
相关介绍:
http://www.cnblogs.com/zenfly/archive/2012/02/10/2345196.html
http://gundumw100.iteye.com/blog/2160467
@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.sample_main);if (getIntent() != null) {handleIntent(getIntent());}}
@Overrideprotected void onNewIntent(Intent intent) {handleIntent(intent);}
private void handleIntent(Intent intent) {if (Intent.ACTION_SEARCH.equals(intent.getAction())) {String query = intent.getStringExtra(SearchManager.QUERY);Bundle bundle = new Bundle();bundle.putString(QUERY_KEY, query);ContactablesLoaderCallbacks loaderCallbacks = new ContactablesLoaderCallbacks(this);getLoaderManager().restartLoader(CONTACT_QUERY_LOADER, bundle, loaderCallbacks);}}