在Android中所集成的HttpClient是(org.apache.http.*)
Android 实现Post向服务器提交数据
熟悉web编程的都很了解get和post这两种传递表单数据的方法。
这里不具体介绍get和post的区别,如需了解请参考:http://www.2cto.com/kf/201112/114558.html
所谓的get传递数据也是我们最常见的一种,如http://127.0.0.1/index.php?param=androidyue,这种方式直接显示在url中,因此很不安全,
而使用post传递数据则不会直接暴露出来,相对来说更加安全一些。post传递也需要key和value。
以下是Android客户端的代码:
package irdc.EX08_01;
/*必需引用apache.http相关类来建立HTTP联机*/
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.HttpGet;
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;
/*必需引用java.io 与java.util相关类来读写档案*/
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class EX08_01 extends Activity
{
/*声明两个Button对象,与几个TextView对象*/
private Button mButton1,mButton2;
private TextView mTextView1;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
/*透过findViewById建构巳建立TextView与Button对象*/
mButton1 =(Button) findViewById(R.id.myButton1);
mButton2 =(Button) findViewById(R.id.myButton2);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
/*设定OnClickListener来监听OnClick事件*/
mButton1.setOnClickListener(new Button.OnClickListener()
{
/*覆写onClick事件*/
@Override
public void onClick(View v)
{
/*宣╳网份?串*/
//String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Post/index.php";
String uriAPI = "http://192.168.1.101:89/Android_Web/post_get.aspx";
/*建立HTTP Post联机*/
HttpPost httpRequest = new HttpPost(uriAPI);
/*
* Post运囫传送变量必须用NameValuePair[]阵在储?
*/
List <NameValuePair> params = new ArrayList <NameValuePair>();
params.add(new BasicNameValuePair("str", "I am Post String我们"));
try
{
/*发叨HTTP request*/
httpRequest.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
/*取得HTTP response*/
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
/*若状态码为200 ok*/
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
/*取叨并应?串*/
String strResult = EntityUtils.toString(httpResponse.getEntity());
mTextView1.setText(strResult);
}
else
{
mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
mButton2.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View v)
{
// TODO Auto-generated method stub
/*宣╳网份?串*/
//String uriAPI = "http://www.dubblogs.cc:8751/Android/Test/API/Get/index.php?str=I+am+Get+String";
String uriAPI="http://localhost:2094/web_test/Android_Web/post_get.aspx?str=i am nifangzhen";
/*建立HTTP Get联机*/
HttpGet httpRequest = new HttpGet(uriAPI);
try
{
/*发叨HTTP request*/
HttpResponse httpResponse = new DefaultHttpClient().execute(httpRequest);
/*若状态码为200 ok*/
if(httpResponse.getStatusLine().getStatusCode() == 200)
{
/*取叨并应?串*/
String strResult = EntityUtils.toString(httpResponse.getEntity());
/*?除?余?元*/
strResult = eregi_replace("(\r\n|\r|\n|\n\r)","",strResult);
mTextView1.setText(strResult);
}
else
{
mTextView1.setText("Error Response: "+httpResponse.getStatusLine().toString());
}
}
catch (ClientProtocolException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (IOException e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
catch (Exception e)
{
mTextView1.setText(e.getMessage().toString());
e.printStackTrace();
}
}
});
}
/* 告订?串取占函数 */
public String eregi_replace(String strFrom, String strTo, String strTarget)
{
String strPattern = "(?i)"+strFrom;
Pattern p = Pattern.compile(strPattern);
Matcher m = p.matcher(strTarget);
if(m.find())
{
return strTarget.replaceAll(strFrom, strTo);
}
else
{
return strTarget;
}
}
}