Advertisement
Guest User

Untitled

a guest
Apr 30th, 2015
393
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.66 KB | None | 0 0
  1. package com.zezombye.caddit;
  2.  
  3. import android.R;
  4. import android.R.id;
  5. import android.os.Bundle;
  6. import android.support.v7.app.ActionBarActivity;
  7. import android.util.Log;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.widget.TextView;
  11.  
  12. import java.io.IOException;
  13. import java.io.InputStream;
  14. import java.net.HttpURLConnection;
  15. import java.net.URL;
  16.  
  17.  
  18. public class MainActivity extends ActionBarActivity {
  19.  
  20.     @Override
  21.     protected void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         setContentView(R.layout.activity_main);
  24.     }
  25.  
  26.  
  27.  
  28.     @Override
  29.     public boolean onCreateOptionsMenu(Menu menu) {
  30.         // Inflate the menu; this adds items to the action bar if it is present.
  31.         getMenuInflater().inflate(R.menu.menu_main, menu);
  32.         return true;
  33.     }
  34.  
  35.     @Override
  36.     public boolean onOptionsItemSelected(MenuItem item) {
  37.         // Handle action bar item clicks here. The action bar will
  38.         // automatically handle clicks on the Home/Up button, so long
  39.         // as you specify a parent activity in AndroidManifest.xml.
  40.         int id = item.getItemId();
  41.  
  42.         //noinspection SimplifiableIfStatement
  43.         if (id == R.id.action_settings) {
  44.             return true;
  45.         }
  46.  
  47.         return super.onOptionsItemSelected(item);
  48.     }
  49. }
  50.  
  51.  
  52.  
  53. public class HttpExampleActivity extends Activity {
  54.     private static final String DEBUG_TAG = "HttpExample";
  55.     private EditText urlText;
  56.     private TextView textView;
  57.  
  58.     @Override
  59.     public void onCreate(Bundle savedInstanceState) {
  60.         super.onCreate(savedInstanceState);
  61.         setContentView(R.layout.main);
  62.         urlText = (EditText) findViewById(R.id.myUrl);
  63.         textView = (TextView) findViewById(R.id.myText);
  64.     }
  65.  
  66.     // When user clicks button, calls AsyncTask.
  67.     // Before attempting to fetch the URL, makes sure that there is a network connection.
  68.     public void myClickHandler(View view) {
  69.         // Gets the URL from the UI's text field.
  70.         String stringUrl = urlText.getText().toString();
  71.         ConnectivityManager connMgr = (ConnectivityManager)
  72.                 getSystemService(Context.CONNECTIVITY_SERVICE);
  73.         NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
  74.         if (networkInfo != null && networkInfo.isConnected()) {
  75.             new DownloadWebpageTask().execute(stringUrl);
  76.         } else {
  77.             textView.setText("No network connection available.");
  78.         }
  79.     }
  80.  
  81.     // Uses AsyncTask to create a task away from the main UI thread. This task takes a
  82.     // URL string and uses it to create an HttpUrlConnection. Once the connection
  83.     // has been established, the AsyncTask downloads the contents of the webpage as
  84.     // an InputStream. Finally, the InputStream is converted into a string, which is
  85.     // displayed in the UI by the AsyncTask's onPostExecute method.
  86.     private class DownloadWebpageTask extends AsyncTask<String, Void, String> {
  87.         @Override
  88.         protected String doInBackground(String... urls) {
  89.  
  90.             // params comes from the execute() call: params[0] is the url.
  91.             try {
  92.                 return downloadUrl(urls[0]);
  93.             } catch (IOException e) {
  94.                 return "Unable to retrieve web page. URL may be invalid.";
  95.             }
  96.         }
  97.         // onPostExecute displays the results of the AsyncTask.
  98.         @Override
  99.         protected void onPostExecute(String result) {
  100.             textView.setText(result);
  101.         }
  102.     }
  103. }
  104.  
  105.     // Given a URL, establishes an HttpUrlConnection and retrieves
  106. // the web page content as a InputStream, which it returns as
  107. // a string.
  108.     private String downloadUrl(String myurl) throws IOException {
  109.         InputStream is = null;
  110.         // Only display the first 500 characters of the retrieved
  111.         // web page content.
  112.         int len = 500;
  113.  
  114.         try {
  115.             URL url = new URL("http://i.reddit.com/");
  116.             HttpURLConnection conn = (HttpURLConnection) url.openConnection();
  117.             conn.setReadTimeout(10000 /* milliseconds */);
  118.             conn.setConnectTimeout(15000 /* milliseconds */);
  119.             conn.setRequestMethod("GET");
  120.             conn.setDoInput(true);
  121.             // Starts the query
  122.             conn.connect();
  123.             int response = conn.getResponseCode();
  124.             Log.d(DEBUG_TAG, "The response is: " + response);
  125.             is = conn.getInputStream();
  126.  
  127.             // Convert the InputStream into a string
  128.             String contentAsString = readIt(is, len);
  129.             return contentAsString;
  130.  
  131.             String contentAsString;
  132.                 public void onCreate;(Bundle savedInstanceState;) {
  133.                 setContentView(R.layout.main);
  134.                 super.onCreate(savedInstanceState);
  135.  
  136.                 txtView=(TextView)findViewById(R.id.my_text_view);
  137.                 txtwidth = (TextView)findViewById(R.id.viewwidth);
  138.                 hello="This is my first project";
  139.  
  140.  
  141.                 mybtn.setOnClickListener(this);
  142.                 txtView.setText(hello);}
  143.  
  144.             String displaythisgoddammit = "display this goddammit";
  145.             private TextView text = (TextView) findViewById(R.id.my_text_view);
  146.             text.setText(displaythisgoddammit);
  147.  
  148.             // Makes sure that the InputStream is closed after the app is
  149.             // finished using it.
  150.  
  151.  
  152.         } finally {
  153.             if (is != null) {
  154.                 is.close();
  155.             }
  156.         }
  157.     }
  158.  
  159.     @Override
  160.     public void onCreate(Bundle savedInstanceState) {
  161.         super.onCreate(savedInstanceState);
  162.         setContentView(R.layout.activity_main);
  163.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement