1、首先输入代码
public void SendByHttpClient(final String id, final String pw){
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpClient httpclient=new DefaultHttpClient()
HttpPost httpPost=new HttpPost("http://web应用部署服务器上的IP地址:/HttpClientDemo/Login")//服务器地址,指向Servlet
List<NameValuePair>params=new ArrayList<NameValuePair>()//将id和pw装入list
params.add(new BasicNameValuePair("ID",id))
params.add(new BasicNameValuePair("PW",pw))
final UrlEncodedFormEntity entity=new UrlEncodedFormEntity(params,"utf-8")//以UTF-8格式发送
httpPost.setEntity(entity)
HttpResponse httpResponse= httpclient.execute(httpPost)
if(httpResponse.getStatusLine().getStatusCode()==200)//在200毫秒之内接收到返回值
2、然后再输入下方的代码:
{
HttpEntity entity=httpResponse.getEntity()
String response=EntityUtils.toString(entity1, "utf-8")//以UTF-8格式解析
Message message=new Message()
message.what=USER_LOGIN
message.obj=response
handler.sendMessage(message)使用Message传递消息给线程
}
}
catch (Exception e) {
e.printStackTrace()
}
}
}).start()
}3、最终,测试结果图,如下:
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)