android 网络之 httppost

android中使用http协议进行通信

以下的一个例子,是我G06的android测试代码

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
 
 private static final int STATE_21 = 0x21;
 private static final int STATE_22 = 0x22;
 private static final int STATE_01 = 0x01;
 private static final int STATE_02 = 0x02;
 private static final int STATE_31 = 0x31;
 
 private static int iCurrentState = STATE_21;
 
 private static final String strUrl = "http://XXX.XXX.XXX.XXX:8080/terminal/gprs";//00 11s
 private static final String strUserAgent = "13800138000";
 private Button btn;
 private Button btn2;
 private TextView textView;
 private EditText editText;
 private String strData;
 
 private Handler handler = new Handler()
 {
  @Override
  public void handleMessage(Message msg )
  {   
   switch(msg.what)
   {
   case STATE_22:
    ConstructHttpBody("[201202201357250002,T01,29,13486108318@4@1;2;3;4@1@1@1@1]");
    break;
    
   case STATE_02:
    ConstructHttpBody("[201202201357250002,T31,1,0]");
    break;
   }
  }
 };
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        InitWidget();
      
        btn2.setOnClickListener( new Button.OnClickListener()
        {
         @Override
         public void onClick(View v )
         {
          try
          {
           if( iCurrentState == STATE_21 )
            ConstructHttpBody("[201201172244290000,T21,55,16@222222333335555@460020863831969@89860048111151031969]");
          }
          catch(Exception e )
          {
           Log.e("XXX",e.toString());
          }
         }
        }
        ); 
    }

    private void ConstructHttpBody(String strBody)
    {
     HttpPost httpRequest = new HttpPost(strUrl);
  httpRequest.setHeader("User Agent",strUserAgent);
  
  try
  {
   StringEntity strEntity = new StringEntity(strBody,HTTP.UTF_8);
   httpRequest.setEntity(strEntity);
   HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
   
   int ret = httpResponse.getStatusLine().getStatusCode();
      
   if(ret == 200 )
   {
    Message msg = new Message();
    String str = EntityUtils.toString(httpResponse.getEntity());
    
    textView.setText(str);
    
    if( str.indexOf("T22",0) > 0 )
    {
     msg.what = STATE_22;
     msg.obj = str;
    }
    else if( str.indexOf("T02",0) > 0 )
    {
     msg.what = STATE_01;
     msg.obj = str;
    }    
    
    handler.sendMessage(msg);
   }
   else
   {
    Toast.makeText(MainActivity.this,""+ ret ,Toast.LENGTH_SHORT).show();
   }
  }
  catch(Exception e )
  {
  }
    }

   
    private void InitWidget()
    {
     btn = (Button)findViewById(R.id.btn);
     btn2 = (Button)findViewById(R.id.btn2);
     textView = (TextView)findViewById(R.id.text);
     editText = (EditText)findViewById(R.id.edit);
    }
   
    @Override
    protected void onDestroy()
    {
     super.onDestroy();
     System.gc();
     System.exit(0);
    }
}

posted on 2012-02-27 18:28  景树园  阅读(277)  评论(0编辑  收藏  举报

导航