Android Open Source - AndroidPostTool Main Activity






From Project

Back to project page AndroidPostTool.

License

The source code is released under:

Apache License

If you think the Android project AndroidPostTool 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.yameguun.posttext;
/*  w  w w. ja  va 2s.co m*/
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;

import android.app.Activity;
import android.app.ProgressDialog;
import android.net.http.AndroidHttpClient;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
  
  private boolean clickFlg = false;
  private EditText title;
  private EditText contents;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    
    title = (EditText) this.findViewById(R.id.title);
    contents = (EditText) this.findViewById(R.id.contents);
    
    Button button = (Button) this.findViewById(R.id.button);
    button.setOnClickListener(new OnClickListener(){
      @Override
      public void onClick(View arg0) {
        clickFlg = true;
        PostTask task = new PostTask();
        task.execute();
      }
    });
  }
  
  class PostTask extends AsyncTask<Void,Void,Boolean>{
    
    private ProgressDialog progress;
    
    @Override
    protected void onPreExecute() {
      progress = new ProgressDialog(MainActivity.this);
      progress.setMessage("???M??");
      progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
      progress.setCancelable(false);
      progress.show();
    }
    
    @Override
    protected Boolean doInBackground(Void... params) {

      List<NameValuePair> namePairs = new ArrayList<NameValuePair>();
      namePairs.add(new BasicNameValuePair("TITLE", title.getText().toString()));
      namePairs.add(new BasicNameValuePair("CONTENTS", contents.getText().toString()));

      HttpParams httpParams = new BasicHttpParams();
      HttpConnectionParams.setConnectionTimeout(httpParams, 100000);
      HttpConnectionParams.setSoTimeout(httpParams, 3000000);

      AndroidHttpClient client = AndroidHttpClient.newInstance("Android UserAgent");
      HttpPost post = new HttpPost();
      post.setParams(httpParams);
      
      boolean flg = true;
      
      try {
        post.setURI(new URI(""));
        post.setEntity(new UrlEncodedFormEntity(namePairs, "UTF-8"));
        HttpResponse res = client.execute(post);
        int status = res.getStatusLine().getStatusCode();
        
        if (status != HttpStatus.SC_OK){
          flg = false;
        }
        
      }catch(Exception e){
        flg = false;
      }finally {
        client.getConnectionManager().shutdown();// ?R?l?N?V????????f
        client.close();
      }

      return flg;
    }
    
    @Override
    protected void onPostExecute(Boolean result){
      
      if(result){
        title.setText("");
        contents.setText("");
      }
      
      if(progress.isShowing()){
        progress.dismiss();
      }
      
      clickFlg = false;
    }
  }
}




Java Source Code List

com.yameguun.posttext.MainActivity.java