返回顶部
首页 > 资讯 > 移动开发 >android平台HttpGet、HttpPost请求实例
  • 200
分享到

android平台HttpGet、HttpPost请求实例

Android 2022-06-06 10:06:16 200人浏览 泡泡鱼
摘要

使用HttpClient中的HttpGet()方法进行http通信的实例: 代码如下: package me.Gogogoog;  import java.io.

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

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{
 
    @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通信的实例:
代码如下:
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{ 

   
    @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:向服务器提供数据之get、post方式android AsynTask处理返回数据和AsynTask使用get,post请求Android中post和get的提交方式【三种】Android中使用OkHttp包处理HTTP的get和post请求的方法Android中使用HttpURLConnection实现GET POST JSON数据与下载图片android使用url connection示例(get和post数据获取返回数据)Android发送GET与POST请求的DEMO详解android之HttpPost&HttpGet使用方法介绍Android HttpClient GET或者POST请求基本使用方法安卓GET与POST网络请求的三种方式


--结束END--

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

本文链接: https://lsjlt.com/news/27115.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作