欢迎来到代码驿站!

Android代码

当前位置:首页 > 移动开发 > Android代码

android平台HttpGet、HttpPost请求实例

时间:2020-11-29 10:48:28|栏目:Android代码|点击:

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

复制代码 代码如下:

/** 
 *description:Android HttpGet() 
 *authour:YanEr?Gates 
 *website:https://www.jb51.net
 */

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;   
        //创建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:https://www.jb51.net
*/
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"; 

        //创建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 Path绘制贝塞尔曲线实现QQ拖拽泡泡

本文标题:android平台HttpGet、HttpPost请求实例

本文地址:http://www.codeinn.net/misctech/27483.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有