HttpClient 和 HttpURLConnection两种请求方式的写法
2016-04-15 18:57
603 查看
public class RegisterAsyncTaskGet extends AsyncTask<String, Void, String>{ @Override protected String doInBackground(String... params) { try { URL url=new URL(params[0]); HttpURLConnection conn=(HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.setConnectTimeout(15000); conn.setReadTimeout(15000); conn.connect(); if(conn.getResponseCode()==200){ InputStream in=conn.getInputStream(); return StringUtils.getStringByInputStream(in); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } } | public class RegisterAsyncTaskPost extends AsyncTask<String, Void, String>{ @Override protected String doInBackground(String... params) { try { URL url=new URL(params[0]); HttpURLConnection conn=(HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); DataOutputStream stream=new DataOutputStream(conn.getOutputStream()); stream.writeBytes("uname"+params[1]+"&upassword"+params[2]); conn.setConnectTimeout(15000); conn.setReadTimeout(15000); conn.connect(); if(conn.getResponseCode()==200){ InputStream in=conn.getInputStream(); return StringUtils.getStringByInputStream(in); } } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } } |
---|---|
public class MyHttpClientGet extends AsyncTask<String, Void, String>{ /** * 用HttpClient GET方式 发起请求 */ @Override protected String doInBackground(String...params) { // TODO Auto-generated method stub //1.创建HttpClient HttpClient client=new DefaultHttpClient(); //2.创建一个GET请求 HttpGet get=new HttpGet(params[0]); //3.向服务端发起请求,=前为接收 响应,=后为发起请求 try { HttpResponse httpresponse=client.execute(get); //4.获取响应回来的结果 if(httpresponse.getStatusLine().getStatusCode()==200){ HttpEntity entity=httpresponse.getEntity(); //5.获取我们想要的结果 String str=EntityUtils.toString(entity, "GBK"); return str; } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } } | public class MyHttpClientPost extends AsyncTask<String, Void, String> { /** * 用HttpClient POST方式 发起请求 */ @Override protected String doInBackground(String...params) { // TODO Auto-generated method stub // 1.创建HttpClient HttpClient client = new DefaultHttpClient(); // 2.创建一个POST请求 HttpPost post = new HttpPost(params[0]); // 3.封装post的请求参数 List<NameValuePair> pairs = new ArrayList<NameValuePair>(); pairs.add(new BasicNameValuePair("uname", params[1])); pairs.add(new BasicNameValuePair("upassword", params[2])); UrlEncodedFormEntity entity1; try { entity1 = new UrlEncodedFormEntity(pairs, "GBK"); post.setEntity(entity1);// 将数据保存在ENTITY中 // 4.向服务端发起请求,=前面为接收响应,=后面为发起请求 HttpResponse httpresponse = client.execute(post); // 5.获取响应回来的结果 if (httpresponse.getStatusLine().getStatusCode() == 200) { HttpEntity entity = httpresponse.getEntity(); // 6.获取我们想要的结果 String str = EntityUtils.toString(entity, "GBK"); return str; } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return null; } } |
/** * 内部类继承 异步任务类 Override重写onPostExecute() 方法 对返回结果进一步处理 * * @author zjp * */ class LoginGet extends MyHttpClientGet { @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); if (!result.equals("error") && result != null) {// 返回值不为空或error说明成功 Gson g = new Gson(); Results re = g.fromJson(result, Results.class); Toast.makeText(MainActivity.this, re.getInfo().toString(), 0) .show(); } else {// 失败 Toast.makeText(MainActivity.this, "error", 0).show(); } } } | class LoginPost extends MyHttpClientPost { @Override protected void onPostExecute(String result) { // TODO Auto-generated method stub super.onPostExecute(result); if (!result.equals("error") && result != null) {// 返回值不为空或error说明成功 Gson g = new Gson(); Results re = g.fromJson(result, Results.class); Toast.makeText(MainActivity.this, re.getInfo().toString(), 0) .show(); } else {// 失败 Toast.makeText(MainActivity.this, "error", 0).show(); } } } |
public void login(View v) { String name = et_name.getText().toString().trim(); String pwd = et_pwd.getText().toString().trim(); LoginGet re = new LoginGet();//创建内部类对象 re.execute(url + "?uname=" + name + "&upassword=" + pwd);//提交请求 } | public void loginP(View v) { String name = mNameEt.getText().toString().trim(); String pwd = mPwdEt.getText().toString().trim(); LoginPost lp = new LoginPost(); lp.execute(url,name,pwd); } |
相关文章推荐
- HorizontalScrollView动态添加子view,并且设置每个子view的点击事件
- HTTP简单介绍
- iOS AFNetworking http 报错
- IOS -Xcode7 使用NSURLSession发送HTTP请求报错
- 又论Node.js的HTTP模块之深入理解
- Http请求get、post工具类
- Http请求get、post工具类
- TCP/IP 协议相关
- 【原创】网站抓包HttpWebRequest不返回Javascript生成的Cookie的解决办法
- 基于HBASE的数据包捕获、存储、回溯工具
- C++发送消息格式为JSON的http请求
- C++ 用libcurl库进行http通讯网络编程
- XMLHttpRequest实现终止请求和超时
- Iptables防火墙使用
- 结合Wireshark捕获分组深入理解TCP/IP协议栈之HTTP协议
- ajax原理及xmlhttpRequest
- smokeping对网络进行监控
- 可视化理解卷积神经网络-ECCV 2014
- MFC检测网络连接和ping IP地址
- jQuery ajax 请求HttpServlet返回[HTTP/1.1 405 Method not allowed]