Android Open Source - pi07http Main Activity






From Project

Back to project page pi07http.

License

The source code is released under:

MIT License

If you think the Android project pi07http 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.example.pi07http;
// ww  w.  j  a v a 2 s . c  o  m
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    // ???????????
    findViewById(R.id.progressBar1).setVisibility(View.INVISIBLE);
  }

  @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;
  }

  HttpTask mTask;
  public void buttonMethod(View button){
    // ??????
    if(mTask != null)return;
    mTask = new HttpTask();
    // ?????
    mTask.execute("http://google.co.jp/");
  }
  
  // ??????onPostExecute???????????
  private void showResult(String result){
    TextView t = (TextView)findViewById(R.id.textView1);
    t.setText(result);
  }
  
  // ?????
  class HttpTask extends AsyncTask<String, Integer, String>{
    String mUrl = "";
    
    // ???????????
    @Override
    protected void onPreExecute() {
      // ????????
      findViewById(R.id.progressBar1).setVisibility(View.VISIBLE);
      // TODO Auto-generated method stub
      super.onPreExecute();
    }

    // ?????????????????????
    // ?AsyncTask ??? cancel ??????????????doInBackground ??? InterruptedException ????????????????????????????? onPostExecute ???????????? onCancelled ??????????????????
    @Override
    protected String doInBackground(String... params) {
      if(params.length == 0)return null;
      String result = null;
      try{
        mUrl = params[0];
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet request = new HttpGet(mUrl);
        HttpResponse response = client.execute(request);
        if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
          result = EntityUtils.toString(response.getEntity());
        }
        else{
          result = "NOT OK";
        }
      }
      catch(Exception ex){
        result = ex.toString();
      }
      return result;
    }

    // ??????????????UI?????????????????
    @Override
    protected void onProgressUpdate(Integer... values) {
      // TODO Auto-generated method stub
      super.onProgressUpdate(values);
    }
    
    // ????????????????????????????????????UI???????????????????????????????????????????????
    @Override
    protected void onPostExecute(String result) {
      // ?????
      showResult(result);
      mTask = null;
      // ???????????
      findViewById(R.id.progressBar1).setVisibility(View.INVISIBLE);
      super.onPostExecute(result);
    }

    // ????????????????????cancel?????????????????doInBackground???????????????????????
    @Override
    protected void onCancelled() {
      // TODO Auto-generated method stub
      super.onCancelled();
    }

  }
}




Java Source Code List

com.example.pi07http.MainActivity.java