Android通过http与服务器端交互

一、post方式
        String uriAPI = "http://192.168.2.229:8088/YichaMarket/soft/list.action";
      
        HttpPost httpRequest = new HttpPost(uriAPI);
      
        List <NameValuePair> params = new ArrayList <NameValuePair>();
        params.add(new BasicNameValuePair(
"customName", "lunzi"));
        try
        {
        
          httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
        
          HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
          
          if(httpResponse.getStatusLine().getStatusCode() == 200)  
          {
          
            String strResult = EntityUtils.toString(httpResponse.getEntity());
            
            JSONObject json = new JSONObject(strResult);
            
            mTextView1.setText(json.getString(
"newName"));
          }
          else
          {
            mTextView1.setText(
"Error Response: "+httpResponse.getStatusLine().toString());
          }
二、get方式
String uriAPI =
"http://192.168.2.229:8088/YichaMarket/soft/list.action?customName=lunzi";
        HttpGet httpRequest = new HttpGet(uriAPI);
        try
        {
          HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
          if(httpResponse.getStatusLine().getStatusCode() == 200)  
          {
            String strResult = EntityUtils.toString(httpResponse.getEntity());
          
          }
          else
          {
            
          }
lunzi   2011-06-16 17:44:56 评论:3   阅读:3512   引用:0
呵呵 @2011-06-21 18:51:31  小鑫
太搞了,1l肯定是没搞过一天android~ 不然不会连这个都不知道~
无题 @2011-06-17 23:44:27  lunzi
这孩子,我骗谁了?android类库自动集成了HttpClient,然后android通过这个类库和server打交道,有问题吗?
无题 @2011-06-17 17:48:06  meiking
骗子,什么Android啊,这不是HttpClient嘛。

发表评论>>

署名发表(评论可管理,不必输入下面的姓名)

姓名:

主题:

内容: 最少15个,最长1000个字符

验证码: (如不清楚,请刷新)

Copyright@2004-2010 powered by YuLog