Android HttpPost表单访问接口
来源:互联网 发布:相似矩阵的几相等 编辑:程序博客网 时间:2024/06/05 23:53
这是我常用的来进行接口调用的方法,非常的好用,希望能给需要的朋友带来便利。
// 调取接口发送数据private void getData(final String url, final String id) {// 获取数据,把线程写入了其中new Thread() {public void run() {Message msg = vipmyHandler.obtainMessage();String type = "";try {urlclient = new UrLClient();List<NameValuePair> content = new ArrayList<NameValuePair>();content.add(new BasicNameValuePair("suggestcontent", editText.getText().toString()));type = urlclient.postFormsendCookiesData(url, content, FanKuiActivity.this);if(type.equals("success")){msg.what=0;}else{msg.what=1;}} catch (Exception e) {e.printStackTrace();Toast.makeText(FanKuiActivity.this, "参数传递错误",Toast.LENGTH_SHORT).show();System.out.println("*************************" + "参数传递错误"+ "**********************************");}vipmyHandler.sendMessage(msg);}}.start();}@Overridepublic void onDestroy() {// TODO Auto-generated method stubvipmyHandler.removeCallbacksAndMessages(null);super.onDestroy();}Handler vipmyHandler = new Handler() {@SuppressLint("HandlerLeak")public void handleMessage(Message msg) {switch (msg.what) {case 0:String httpstr = urlclient.getInput();if (httpstr == null || httpstr.equals("")) {return;}error_PopupWindows = new error_PopupWindows(FanKuiActivity.this, titlebar, "反馈成功,谢谢您的意见!");break;case 1:error_PopupWindows = new error_PopupWindows(FanKuiActivity.this, titlebar, "提交失败");break;}super.handleMessage(msg);}};
// post登录连接 public String postFormsendCookiesData(String url, List<NameValuePair> params, Context urlcontext) { String isConnect = ""; // 第一步,创建HttpPost对象 HttpPost httpPost = new HttpPost(UrlVO.Host_Url + url); HttpResponse httpResponse = null; try { // 设置httpPost请求参数 httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8)); DefaultHttpClient httpclient = new DefaultHttpClient(); if (!UrlVO.getShareData("JSESSIONID", urlcontext).equals("")) { httpPost.setHeader("Cookie", UrlVO.getShareData("JSESSIONID", urlcontext)); } // 请求超时 httpclient.getParams().setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, 20000); // 读取超时 httpclient.getParams().setParameter( CoreConnectionPNames.SO_TIMEOUT, 20000); httpResponse = httpclient.execute(httpPost); System.out.println(httpResponse.getStatusLine().getStatusCode()); if (httpResponse.getStatusLine().getStatusCode() == 200) { // 第三步,使用getEntity方法活得返回结果 String result = EntityUtils.toString(httpResponse.getEntity()); setInput(result); System.out.println("result:" + result); isConnect = "success"; } else { setWrongcode(String.valueOf(httpResponse.getStatusLine() .getStatusCode()), urlcontext); } } catch (ClientProtocolException e) { e.printStackTrace(); Toast.makeText(urlcontext, "读取超时或连接错误", Toast.LENGTH_SHORT).show(); } catch (IOException e) { e.printStackTrace(); Toast.makeText(urlcontext, "网络连接失败", Toast.LENGTH_SHORT).show(); } return isConnect; }
主要的功能我都已经放在这里了,只要进行简单的调用就可以了
0 0
- Android HttpPost表单访问接口
- android---HttpPost访问服务器
- 使用httpPost进行访问远程接口
- Android HttpPost
- httpclient提交表单 访问接口
- android的httppost方法访问本地web服务(一)
- android的httppost方法访问本地web服务(二)
- HttpPost 访问网络
- httppost方式访问网络数据
- android 用httpPost方法递交表单数据的两种方法.
- android访问I2C接口
- Android gprs httppost 404
- android之HttpPost&HttpGet
- Android:HttpGet与HttpPost
- android httpPost 的请求
- Android日常整理(二)---博客园接口、 HttpGet类和HttpPost类、ProgressDialog、dispatchKeyEvent
- Android中使用HttpGet和HttpPost访问HTTP资源(一)
- Android中使用HttpGet和HttpPost访问HTTP资源和区别
- 如何查看url的IP地址,以及IP地址所属区域
- 设计模式之观察者模式
- 项目练习--js操作删除DIV层中的子节点
- 有关spring的知识-----bean的实例化方法
- 十个最值得阅读学习的C开源项目代码
- Android HttpPost表单访问接口
- J2SE内容小结
- 模糊(Blur)的学习与体会(二)
- Meta标签详解
- A teeny tiny example about Function Objects
- 学习资源
- 文章标题
- 获取表单对象,得三种方法
- Tomcat启动不了的问题