Android--Post向服务器提交数据

来源:互联网 发布:ubuntu网络重启失败 编辑:程序博客网 时间:2024/05/18 03:35

Android 实现Post向服务器提交数据

android客户端代码:

 

public static String postData(String sUrl){

String destUrl = "";

destUrl = sUrl;

String sResult = "";

//instantiate HttpPost object from the url address

HttpEntityEnclosingRequestBase httpRequest =new HttpPost(destUrl);

//the post name and value must be used asNameValuePair

  List params = newArrayList();

  params.add(newBasicNameValuePair("id", "111111"));

  params.add(newBasicNameValuePair("title", "ttttttttttt"));

  params.add(newBasicNameValuePair("content","ccccccccccccccc"));

  params.add(newBasicNameValuePair("img", "iiiiiiii"));

try {

httpRequest.setEntity(newUrlEncodedFormEntity(params, HTTP.UTF_8));

//execute the post and get the response fromservers

HttpResponse httpResponse = newDefaultHttpClient()

.execute(httpRequest);

if(httpResponse.getStatusLine().getStatusCode() == 200){

//get the result

String strResult =EntityUtils.toString(httpResponse

.getEntity());

sResult = strResult;

System.out.println(strResult);

}else {

System.out.println("ErrorResponse"

+httpResponse.getStatusLine().toString());

}

}catch (Exception e) {

System.out.println("erroroccurs");

}

return sResult;

}

服务器端php代码: