android发送http post请求函数带参数带返回数据 静态类 源码

调用示例 String str=HttpRequest.sendPost("http://supermindsoft.com/test/index.php", "mm=19891107&gn=kmm",3000);


服务器index.php 编码为dos默认

<?php
   if(@$_POST["mm"]=="19891107"&&@$_POST["gn"]=="kmm")
   {            
      echo "111111";
   }
?>

android上用的HttpRequest.java类,加入到工程。再用上面的调用就行。

package com.example.httppost;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Handler;
import android.os.SystemClock;


public class HttpRequest {
	private static String url = "http://supermindsoft.com/test/index.php";
	static List <NameValuePair> params=new ArrayList <NameValuePair>();   
	HttpResponse httpResponse = null; 
	JSONArray jsonArray = null;
	JSONObject jsonObject=null;
	private static String strResult=null;
	private static String title=null;
	
    /**
     * 向指定 URL 发送POST方法的请求
     * 
     * @param url
     *            发送请求的 URL
     * @param param
     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @param timeoutMillis
     * 			     读取超时           
     * @return 所代表远程资源的响应结果
     *  示例String str=HttpRequest.sendPost("http://supermindsoft.com/test/index.php", "mm=19891107&gn=kmm",3000);
     */
    public static String sendPost(String urladdress, String param,int timeoutMillis)
	{
    	title="";
    	url=urladdress;
    	String[] str=param.split("&");
    	for(int i=0;i<str.length;i++)
    	{
    		String[] p=str[i].split("=");
    		 params.add(new BasicNameValuePair(p[0], p[1]));			
    	}
		 new Thread(runa).start();	
		 for(int i=0;i<timeoutMillis;i++)
		 {
			 if(title!="")
				 return title;
			 SystemClock.sleep(1);   //主线程等待
		 }
		 return title;
	}
	
	static Handler handler = new Handler() 
	{
		public void handleMessage(android.os.Message msg) 
		{
			if (msg.what == 0x01) 
			{
				
			}
		};
	};
	private static Runnable runa=new Runnable() {
		
		public void run() {
			// TODO Auto-generated method stub
			 // 第一步,创建HttpPost对象 
	        HttpPost httpRequest = new HttpPost(url);  
	        /* 
	         * NameValuePair实现请求参数的封装 
	        */  
	      
	        //向服务器发送的内容
	       // params.add(new BasicNameValuePair("mm", "19891107"));
	       // params.add(new BasicNameValuePair("gn", "kmm"));	     
	        try  
	        {  
	          /* 添加请求参数到请求对象*/  
	          httpRequest.setEntity(new UrlEncodedFormEntity(params,  HTTP.DEFAULT_CONTENT_CHARSET));//.UTF_8));  
	          /*发送请求并等待响应*/  
	          //HttpClient httpClient=new DefaultHttpClient();

	          HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);  
	         
	          /*若状态码为200 ok*/  
	          if(httpResponse.getStatusLine().getStatusCode() == 200)   
	          {  
	            /*读返回数据*/  
	            strResult = EntityUtils.toString(httpResponse.getEntity());  
//	            try{
//	            	jsonArray=new JSONArray(strResult);
//	            	for(int i=0;i<jsonArray.length();i++)
//	            	{
//	            		jsonObject=jsonArray.getJSONObject(i);
//	            		title=jsonObject.getString("Title");
//	            	}
//	            }
//	            catch(Exception e)
//	            {
//	            	
//	            }	            
	            title=strResult;
	            handler.sendEmptyMessage(0x01); 
	          }  
	          else  
	          {  
	        	  strResult=httpResponse.getStatusLine().toString();  
	        	  handler.sendEmptyMessage(0x02);
	          }  
	        }  
	        catch (ClientProtocolException e)  
	        {   
	          //tvs.setText(e.getMessage().toString());  
	          e.printStackTrace(); 
	          handler.sendEmptyMessage(0x03);
	        }  
	        catch (IOException e)  
	        {   
	          //tvs.setText(e.getMessage().toString());  
	          e.printStackTrace(); 
	          handler.sendEmptyMessage(0x04);
	        }  
	        catch (Exception e)  
	        {   
	          //tvs.setText(e.getMessage().toString());  
	          e.printStackTrace();
	          handler.sendEmptyMessage(0x05);
	        }    
		}

	};
}




java上用的HttpRequest.java  ,此代码在android环境运行得不到数据【不知哪里错误】。

package com.example.httppost;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
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.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONObject;

import android.os.Handler;
import android.os.SystemClock;


public class HttpRequest {
	private static String url = "http://supermindsoft.com/test/index.php";
	static List <NameValuePair> params=new ArrayList <NameValuePair>();   
	HttpResponse httpResponse = null; 
	JSONArray jsonArray = null;
	JSONObject jsonObject=null;
	private static String strResult=null;
	private static String title=null;
	
    /**
     * 向指定 URL 发送POST方法的请求
     * 
     * @param url
     *            发送请求的 URL
     * @param param
     *            请求参数,请求参数应该是 name1=value1&name2=value2 的形式。
     * @param timeoutMillis
     * 			     读取超时           
     * @return 所代表远程资源的响应结果
     *  示例String str=HttpRequest.sendPost("http://supermindsoft.com/test/index.php", "mm=19891107&gn=kmm",3000);
     */
    public static String sendPost(String urladdress, String param,int timeoutMillis)
	{
    	title="";
    	url=urladdress;
    	String[] str=param.split("&");
    	for(int i=0;i<str.length;i++)
    	{
    		String[] p=str[i].split("=");
    		 params.add(new BasicNameValuePair(p[0], p[1]));			
    	}
		 new Thread(runa).start();	
		 for(int i=0;i<timeoutMillis;i++)
		 {
			 if(title!="")
				 return title;
			 SystemClock.sleep(1);   //主线程等待
		 }
		 return title;
	}
	
	static Handler handler = new Handler() 
	{
		public void handleMessage(android.os.Message msg) 
		{
			if (msg.what == 0x01) 
			{
				
			}
		};
	};
	private static Runnable runa=new Runnable() {
		
		public void run() {
			// TODO Auto-generated method stub
			 // 第一步,创建HttpPost对象 
	        HttpPost httpRequest = new HttpPost(url);  
	        /* 
	         * NameValuePair实现请求参数的封装 
	        */  
	      
	        //向服务器发送的内容
	       // params.add(new BasicNameValuePair("mm", "19891107"));
	       // params.add(new BasicNameValuePair("gn", "kmm"));	     
	        try  
	        {  
	          /* 添加请求参数到请求对象*/  
	          httpRequest.setEntity(new UrlEncodedFormEntity(params,  HTTP.DEFAULT_CONTENT_CHARSET));//.UTF_8));  
	          /*发送请求并等待响应*/  
	          //HttpClient httpClient=new DefaultHttpClient();

	          HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);  
	         
	          /*若状态码为200 ok*/  
	          if(httpResponse.getStatusLine().getStatusCode() == 200)   
	          {  
	            /*读返回数据*/  
	            strResult = EntityUtils.toString(httpResponse.getEntity());  
//	            try{
//	            	jsonArray=new JSONArray(strResult);
//	            	for(int i=0;i<jsonArray.length();i++)
//	            	{
//	            		jsonObject=jsonArray.getJSONObject(i);
//	            		title=jsonObject.getString("Title");
//	            	}
//	            }
//	            catch(Exception e)
//	            {
//	            	
//	            }	            
	            title=strResult;
	            handler.sendEmptyMessage(0x01); 
	          }  
	          else  
	          {  
	        	  strResult=httpResponse.getStatusLine().toString();  
	        	  handler.sendEmptyMessage(0x02);
	          }  
	        }  
	        catch (ClientProtocolException e)  
	        {   
	          //tvs.setText(e.getMessage().toString());  
	          e.printStackTrace(); 
	          handler.sendEmptyMessage(0x03);
	        }  
	        catch (IOException e)  
	        {   
	          //tvs.setText(e.getMessage().toString());  
	          e.printStackTrace(); 
	          handler.sendEmptyMessage(0x04);
	        }  
	        catch (Exception e)  
	        {   
	          //tvs.setText(e.getMessage().toString());  
	          e.printStackTrace();
	          handler.sendEmptyMessage(0x05);
	        }    
		}

	};
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

小黄人软件

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值