Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.zezombye.caddit;
- import android.R;
- import android.R.id;
- import android.os.Bundle;
- import android.support.v7.app.ActionBarActivity;
- import android.util.Log;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.widget.TextView;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.URL;
- public class MainActivity extends ActionBarActivity {
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.menu_main, menu);
- return true;
- }
- @Override
- public boolean onOptionsItemSelected(MenuItem item) {
- // Handle action bar item clicks here. The action bar will
- // automatically handle clicks on the Home/Up button, so long
- // as you specify a parent activity in AndroidManifest.xml.
- int id = item.getItemId();
- //noinspection SimplifiableIfStatement
- if (id == R.id.action_settings) {
- return true;
- }
- return super.onOptionsItemSelected(item);
- }
- }
- public class HttpExampleActivity extends Activity {
- private static final String DEBUG_TAG = "HttpExample";
- private EditText urlText;
- private TextView textView;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- urlText = (EditText) findViewById(R.id.myUrl);
- textView = (TextView) findViewById(R.id.myText);
- }
- // When user clicks button, calls AsyncTask.
- // Before attempting to fetch the URL, makes sure that there is a network connection.
- public void myClickHandler(View view) {
- // Gets the URL from the UI's text field.
- String stringUrl = urlText.getText().toString();
- ConnectivityManager connMgr = (ConnectivityManager)
- getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
- if (networkInfo != null && networkInfo.isConnected()) {
- new DownloadWebpageTask().execute(stringUrl);
- } else {
- textView.setText("No network connection available.");
- }
- }
- // Uses AsyncTask to create a task away from the main UI thread. This task takes a
- // URL string and uses it to create an HttpUrlConnection. Once the connection
- // has been established, the AsyncTask downloads the contents of the webpage as
- // an InputStream. Finally, the InputStream is converted into a string, which is
- // displayed in the UI by the AsyncTask's onPostExecute method.
- private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
- @Override
- protected String doInBackground(String... urls) {
- // params comes from the execute() call: params[0] is the url.
- try {
- return downloadUrl(urls[0]);
- } catch (IOException e) {
- return "Unable to retrieve web page. URL may be invalid.";
- }
- }
- // onPostExecute displays the results of the AsyncTask.
- @Override
- protected void onPostExecute(String result) {
- textView.setText(result);
- }
- }
- }
- // Given a URL, establishes an HttpUrlConnection and retrieves
- // the web page content as a InputStream, which it returns as
- // a string.
- private String downloadUrl(String myurl) throws IOException {
- InputStream is = null;
- // Only display the first 500 characters of the retrieved
- // web page content.
- int len = 500;
- try {
- URL url = new URL("http://i.reddit.com/");
- HttpURLConnection conn = (HttpURLConnection) url.openConnection();
- conn.setReadTimeout(10000 /* milliseconds */);
- conn.setConnectTimeout(15000 /* milliseconds */);
- conn.setRequestMethod("GET");
- conn.setDoInput(true);
- // Starts the query
- conn.connect();
- int response = conn.getResponseCode();
- Log.d(DEBUG_TAG, "The response is: " + response);
- is = conn.getInputStream();
- // Convert the InputStream into a string
- String contentAsString = readIt(is, len);
- return contentAsString;
- String contentAsString;
- public void onCreate;(Bundle savedInstanceState;) {
- setContentView(R.layout.main);
- super.onCreate(savedInstanceState);
- txtView=(TextView)findViewById(R.id.my_text_view);
- txtwidth = (TextView)findViewById(R.id.viewwidth);
- hello="This is my first project";
- mybtn.setOnClickListener(this);
- txtView.setText(hello);}
- String displaythisgoddammit = "display this goddammit";
- private TextView text = (TextView) findViewById(R.id.my_text_view);
- text.setText(displaythisgoddammit);
- // Makes sure that the InputStream is closed after the app is
- // finished using it.
- } finally {
- if (is != null) {
- is.close();
- }
- }
- }
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement