亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

android平臺HttpGet、HttpPost請求實例

 更新時間:2014年05月03日 10:21:28   作者:  
出自網絡搜索引擎巨頭的Android平臺,其對網絡的支持自然不用多說,在Android SDK中已經集成了Apache的HttpClient模塊。使用HttpClient模塊,我們就可以使用HTTP協(xié)議進行網絡連接了

使用HttpClient中的HttpGet()方法進行http通信的實例:

復制代碼 代碼如下:

/** 
 *description:Android HttpGet() 
 *authour:YanEr·Gates 
 *website:http://chabaoo.cn
 */

package me.gogogoog; 

import java.io.IOException;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MyHttpGetActivity extends Activity{
 /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){ 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.result);

        TextView resultText = (TextView) this.findViewById(R.id.resultText);         

        String username="username"; 
        String password="password"; 

        String httpUrl = "http://192.168.1.90:8080/AndroidLogin/loginAction.do?method=login&username="+username+"&password="+password;   
        //創(chuàng)建httpRequest對象
        HttpGet httpRequest = new HttpGet(httpUrl);
  try
  {
   //取得HttpClient對象
   HttpClient httpclient = new DefaultHttpClient();
   //請求HttpClient,取得HttpResponse
   HttpResponse httpResponse = httpclient.execute(httpRequest);
   //請求成功
   if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
   {
    //取得返回的字符串
    String strResult = EntityUtils.toString(httpResponse.getEntity());
    resultText.setText(strResult);
   }
   else
   {
    resultText.setText("請求錯誤!");
   }
  }
  catch (ClientProtocolException e)
  {
   resultText.setText(e.getMessage().toString());
  }
  catch (IOException e)
  {
   resultText.setText(e.getMessage().toString());
  }
  catch (Exception e)
  {
   resultText.setText(e.getMessage().toString());
  }   
    }
}


使用HttpClient中的HttpPost()方法進行http通信的實例:
復制代碼 代碼如下:
/** 
*description:Android HttpPost() 
*authour:YanEr·Gates 
*website:http://chabaoo.cn
*/
package me.gogogoog; 

import java.io.IOException; 

import java.util.ArrayList; 
import java.util.List; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.HttpStatus; 
import org.apache.http.NameValuePair; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.entity.UrlEncodedFormEntity; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.apache.http.message.BasicNameValuePair; 
import org.apache.http.util.EntityUtils; 
import android.app.Activity; 
import android.widget.TextView; 

public class ResultActivity extends Activity{ 

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState){ 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.result); 

        TextView resultText = (TextView) this.findViewById(R.id.resultText);                 
        String username="username"; 
        String password="password"; 
        String httpUrl = "http://192.168.1.90:8080/AndroidLogin/loginAction.do?method=login"; 

        //創(chuàng)建httpRequest對象 
        HttpPost httpRequest = new HttpPost(httpUrl); 

        List<NameValuePair> params = new ArrayList<NameValuePair>(); 
        params.add(new BasicNameValuePair("username", username)); 
        params.add(new BasicNameValuePair("password", password)); 

        try{ 
            //設置字符集 
            HttpEntity httpentity = new UrlEncodedFormEntity(params, "gb2312"); 

            //請求httpRequest 
            httpRequest.setEntity(httpentity); 

            //取得默認的HttpClient 
            HttpClient httpclient = new DefaultHttpClient(); 

            //取得HttpResponse 
            HttpResponse httpResponse = httpclient.execute(httpRequest); 

            //HttpStatus.SC_OK表示連接成功 
            if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK){ 
                //取得返回的字符串 
                String strResult = EntityUtils.toString(httpResponse.getEntity()); 
                resultText.setText(strResult); 
            }else{ 
                resultText.setText("請求錯誤!"); 
            } 
        }catch (ClientProtocolException e){ 
            resultText.setText(e.getMessage().toString()); 
        } catch (IOException e){ 
            resultText.setText(e.getMessage().toString()); 
        }catch (Exception e){ 
            resultText.setText(e.getMessage().toString()); 
        }  
    } 
}

相關文章

  • Android接入阿里云熱修復介紹

    Android接入阿里云熱修復介紹

    大家好,本篇文章主要講的是Android接入阿里云熱修復介紹,感興趣的同學趕快來看一看吧,對你有幫助的話記得收藏一下
    2022-01-01
  • Android實現(xiàn)濾鏡效果ColorMatrix

    Android實現(xiàn)濾鏡效果ColorMatrix

    這篇文章主要為大家詳細介紹了Android實現(xiàn)濾鏡效果ColorMatrix,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-05-05
  • Android 7.0以上版本實現(xiàn)應用內語言切換的方法

    Android 7.0以上版本實現(xiàn)應用內語言切換的方法

    本篇文章主要介紹了Android 7.0以上版本實現(xiàn)應用內語言切換的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • Ubuntu下android adb環(huán)境變量配置方法

    Ubuntu下android adb環(huán)境變量配置方法

    這篇文章主要介紹了Ubuntu下android adb環(huán)境變量配置方法,本文給出了操作步驟,按步驟操作即可,需要的朋友可以參考下
    2015-04-04
  • Android?ViewPager實現(xiàn)左右滑動翻頁效果

    Android?ViewPager實現(xiàn)左右滑動翻頁效果

    這篇文章主要為大家詳細介紹了Android?ViewPager實現(xiàn)左右滑動翻頁效果,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-12-12
  • Android編程之簡單啟動畫面實現(xiàn)方法

    Android編程之簡單啟動畫面實現(xiàn)方法

    這篇文章主要介紹了Android編程之簡單啟動畫面實現(xiàn)方法,結合實例形式較為詳細的分析了開機啟動畫面的制作步驟及布局、Activity跳轉、權限控制等的相關操作技巧,需要的朋友可以參考下
    2016-11-11
  • Android Studio 3.0的下載安裝教程

    Android Studio 3.0的下載安裝教程

    Android Studio從3.0版本新增了許多功能,當然首當其沖就是從3.0版本新增了對 Kotlin 開發(fā)語言的支持,除此之外還有其他一些新功能,今天我們主要來看看如何更新Android Studio 3.0
    2017-10-10
  • Android自定義可左右滑動和點擊的折線圖

    Android自定義可左右滑動和點擊的折線圖

    這篇文章主要為大家詳細介紹了Android自定義可左右滑動和點擊的折線圖,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-04-04
  • Activity 四種啟動模式詳細介紹

    Activity 四種啟動模式詳細介紹

    這篇文章主要介紹了Activity 四種啟動模式詳細介紹的相關資料,需要的朋友可以參考下
    2017-02-02
  • 詳解Android全局異常的捕獲處理

    詳解Android全局異常的捕獲處理

    這篇文章主要為大家介紹了Android全局異常的捕獲處理,為什么要進行捕獲處理,如何進行捕獲處理,想要了解的朋友可以參考一下
    2016-01-01

最新評論