Monkey2HttpRequest.java 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package com.monkey2.lib;
  2. import android.util.Log;
  3. import java.io.ByteArrayOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.io.OutputStream;
  7. import java.net.HttpURLConnection;
  8. import java.net.URL;
  9. import java.util.concurrent.ExecutorService;
  10. import java.util.concurrent.Executors;
  11. import java.util.concurrent.Future;
  12. public class Monkey2HttpRequest{
  13. private static final String TAG = "Monkey2HttpRequest";
  14. HttpURLConnection connection;
  15. int readyState;
  16. boolean sending;
  17. native void onNativeReadyStateChanged( int state );
  18. native void onNativeResponseReceived( String response,int status,int state );
  19. void setReadyState( int state ){
  20. if( state==readyState ) return;
  21. onNativeReadyStateChanged( state );
  22. readyState=state;
  23. }
  24. void open( String req,String url ){
  25. if( readyState!=0 ) return;
  26. try{
  27. URL turl=new URL( url );
  28. connection=(HttpURLConnection)turl.openConnection();
  29. connection.setRequestMethod( req );
  30. setReadyState( 1 );
  31. }catch( IOException ex ){
  32. Log.v( TAG,ex.toString() );
  33. setReadyState( 5 );
  34. }
  35. }
  36. void setHeader( String name,String value ){
  37. if( readyState!=1 || sending ) return;
  38. connection.setRequestProperty( name,value );
  39. }
  40. void send( final String text,final int timeout ){
  41. if( readyState!=1 || sending ) return;
  42. sending=true;
  43. new Thread( new Runnable(){
  44. public void run(){
  45. connection.setConnectTimeout( timeout );
  46. connection.setReadTimeout( timeout );
  47. try{
  48. if( text!=null && text.length()!=0 ){
  49. byte[] bytes=text.getBytes( "UTF-8" );
  50. connection.setDoOutput( true );
  51. connection.setFixedLengthStreamingMode( bytes.length );
  52. OutputStream out=connection.getOutputStream();
  53. out.write( bytes,0,bytes.length );
  54. out.close();
  55. }
  56. InputStream in=connection.getInputStream();
  57. setReadyState( 3 );
  58. byte[] buf=new byte[4096];
  59. ByteArrayOutputStream out=new ByteArrayOutputStream(1024);
  60. for( ;; ){
  61. int n=in.read( buf );
  62. if( n<0 ) break;
  63. out.write( buf,0,n );
  64. }
  65. in.close();
  66. String response=new String( out.toByteArray(),"UTF-8" );
  67. int status=connection.getResponseCode();
  68. onNativeResponseReceived( response,status,4 );
  69. readyState=4;
  70. }catch( IOException ex ){
  71. Log.v( TAG,ex.toString() );
  72. setReadyState( 5 );
  73. }
  74. connection.disconnect();
  75. sending=false;
  76. }
  77. } ).start();
  78. }
  79. void cancel(){
  80. connection.disconnect();
  81. }
  82. }