Android Open Source - firstcodeandroid Main Activity






From Project

Back to project page firstcodeandroid.

License

The source code is released under:

MIT License

If you think the Android project firstcodeandroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.vjia.jokeking;
/* w w w  . java2  s . com*/
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
  private static String classname = MainActivity.class.getName();

  private ListView listView;
  MyAdapter myAdapter;
  /**
   * doctype=json ; doctype=xml
   */
  private static String youdaoDemoURL = "http://fanyi.youdao.com/openapi.do?keyfrom=jokeking&key=1352117898&type=data&doctype=json&version=1.1&q=good";

  private Handler handler = null;
  TextView showYoudaoTV ;
  
  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    GetJoke.getNumber(this);
    listView = (ListView) this.findViewById(R.id.listView);
    myAdapter = new MyAdapter(GetJoke.lists, this);
    listView.setAdapter(myAdapter);

    // test code to know how HTTP codes
    Button httpTestBtn = (Button) this.findViewById(R.id.httpTestBtn);
    showYoudaoTV = (TextView) this.findViewById(R.id.showHTTP);
    httpTestBtn.setOnClickListener(new HttpOnClickListener());
    //????????handler
    handler=new Handler();
  }
  
  Runnable runnableUi = new Runnable(){

    @Override
    public void run() {
      // TODO Auto-generated method stub
      // update UI
      showYoudaoTV.setText(httpContent); 
    }
    
  };
  
  String httpContent;
  
  class HttpOnClickListener implements OnClickListener{
      @Override
      public void onClick(View view) {
        
        showYoudaoTV.setText("Get Loading...");
        new Thread(){
          public void run(){
            handler.post(runnableUi);
            try {
              URL url = new URL(youdaoDemoURL);
              URLConnection conn = url.openConnection();
              InputStream is = conn.getInputStream();
              InputStreamReader isr = new InputStreamReader(is,
                  "utf-8");
              BufferedReader br = new BufferedReader(isr);
              String line;
              while ((line = br.readLine()) != null) {
                Log.i(classname, "line=" + line);
              }
              httpContent="done!";
              br.close();
              isr.close();
              is.close();
            } catch (MalformedURLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
        }.start();
        
        // TODO Auto-generated method stub
//        new AsyncTask<String, Void, Void>() {
//
//          @Override
//          protected Void doInBackground(String... params) {
//            // TODO Auto-generated method stub
//            try {
//              URL url = new URL(params[0]);
//              URLConnection conn = url.openConnection();
//              InputStream is = conn.getInputStream();
//              InputStreamReader isr = new InputStreamReader(is,
//                  "utf-8");
//              BufferedReader br = new BufferedReader(isr);
//              String line;
//              while ((line = br.readLine()) != null) {
//                Log.i(classname, "line=" + line);
//              }
//              /*
//               * ???????????????
//               * showYoudaoTV.setText("Get HTTP done.");
//               * ??Android????view???????????????????????
//               * ????Handler????UI????http://blog.csdn.net/djx123456/article/details/6325983?
//               * 
//               */
//              br.close();
//              isr.close();
//              is.close();
//            } catch (MalformedURLException e) {
//              // TODO Auto-generated catch block
//              e.printStackTrace();
//            } catch (IOException e) {
//              // TODO Auto-generated catch block
//              e.printStackTrace();
//            }
//
//            return null;
//          }
//
//        }.execute(youdaoDemoURL);
        /*
         * ?????????API ??????API????? API key?1352117898 keyfrom?jokeking 
         * ?????2014-12-04  ???????jokeking  ???????http://jokeking.com
         */
      }
  }

  // +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  // @Override
  // public boolean onCreateOptionsMenu(Menu menu) {
  // // Inflate the menu; this adds items to the action bar if it is present.
  // getMenuInflater().inflate(R.menu.main, menu);
  // return true;
  // }

}



/*
  class HttpOnClickListener implements OnClickListener{
      @Override
      public void onClick(View view) {
        // TODO Auto-generated method stub
        new AsyncTask<String, Void, Void>() {

          @Override
          protected Void doInBackground(String... params) {
            // TODO Auto-generated method stub
            try {
              URL url = new URL(params[0]);
              URLConnection conn = url.openConnection();
              InputStream is = conn.getInputStream();
              InputStreamReader isr = new InputStreamReader(is,
                  "utf-8");
              BufferedReader br = new BufferedReader(isr);
              String line;
              while ((line = br.readLine()) != null) {
                Log.i(classname, "line=" + line);
              }
              br.close();
              isr.close();
              is.close();
            } catch (MalformedURLException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
            return null;
          }

        }.execute(youdaoDemoURL);
      }

  }
*/




Java Source Code List

com.example.activitylifecycletest.DialogActivity.java
com.example.activitylifecycletest.MainActivity.java
com.example.activitylifecycletest.NormalActivity.java
com.example.activitytest.FirstActivity.java
com.example.listviewtest.FruitAdapter.java
com.example.listviewtest.Fruit.java
com.example.listviewtest.MainActivity.java
com.jikexueyuan.counttime.MainActivity.java
com.jikexueyuan.getmyphonenumber.GetNumber.java
com.jikexueyuan.getmyphonenumber.MainActivity.java
com.jikexueyuan.getmyphonenumber.MyAdapter.java
com.jikexueyuan.getmyphonenumber.PhoneInfo.java
com.vjia.bookcollector.MainActivity.java
com.vjia.coolweather.MainActivity.java
com.vjia.coolweather.activity.ChooseAreaActivity.java
com.vjia.coolweather.activity.WeatherActivity.java
com.vjia.coolweather.db.CoolWeatherDB.java
com.vjia.coolweather.db.CoolWeatherOpenHelper.java
com.vjia.coolweather.model.City.java
com.vjia.coolweather.model.County.java
com.vjia.coolweather.model.Province.java
com.vjia.coolweather.util.HttpCallbackListener.java
com.vjia.coolweather.util.HttpUtil.java
com.vjia.coolweather.util.Utility.java
com.vjia.helloandroid.FirstActivity.java
com.vjia.helloandroid.HelloAndroidActivity.java
com.vjia.hellonote.AddContent.java
com.vjia.hellonote.MainActivity.java
com.vjia.hellonote.MyAdapter.java
com.vjia.hellonote.NotesDB.java
com.vjia.hellonote.SelectAct.java
com.vjia.jokeking.GetJoke.java
com.vjia.jokeking.HttpCallbackListener.java
com.vjia.jokeking.HttpUtil.java
com.vjia.jokeking.Joke.java
com.vjia.jokeking.MainActivity.java
com.vjia.jokeking.MyAdapter.java
com.vjia.locationtest.MainActivity.java