调用示例 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);
}
}
};
}