android HttpClient将数据提交到服务器
2014-09-02 21:48
351 查看
1、HttpClient 使用方式
2、InputStream转为String方法
public static String loginByClientGet(String username,String password) { try { //打开浏览器 HttpClient client = new DefaultHttpClient(); //输入地址(url) String url = "http://192.168.1.100:8088/Login.ashx?username="+username+"&password="+password; HttpGet httpGet = new HttpGet(url); //按回车(发请求http get请求) HttpResponse response = client.execute(httpGet); //得到相应码 int code = response.getStatusLine().getStatusCode(); if(code==200) { //得到相应实体 HttpEntity entity = response.getEntity(); //得到相应内容 InputStream is = entity.getContent(); return StreamUtil.readInputStream(is); } else { return null; } } catch (Exception e) { e.printStackTrace(); return null; } } public static String loginByClientPost(String username,String password) { try { //打开浏览器 HttpClient client = new DefaultHttpClient(); //输入地址(输入url) String url = "http://192.168.1.100:8088/Login.ashx"; //使用post请求 HttpPost httpPost = new HttpPost(url); //输入指定提交的数据实体 List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("username", username)); params.add(new BasicNameValuePair("password", password)); httpPost.setEntity(new UrlEncodedFormEntity(params)); //得到相应 HttpResponse response = client.execute(httpPost); //得到响应码 int code = response.getStatusLine().getStatusCode(); if(code==200) { //得到相应内容 InputStream is = response.getEntity().getContent(); return StreamUtil.readInputStream(is); } else { return null; } } catch(Exception ex) { ex.printStackTrace(); return null; } }
2、InputStream转为String方法
package com.example.getserverdata.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; public class StreamUtil { public static String readInputStream(InputStream is) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] data = new byte[1024]; int len = 0; try { while((len = is.read(data))!=-1) baos.write(data, 0, len); is.close(); baos.close(); return new String(baos.toByteArray()); } catch (Exception e) { e.printStackTrace(); } return null; } }
相关文章推荐
- Android网络数据之向服务器提交数据的三种方式(get+post+AsyncHttpClient)
- 32.Android端向web端(服务器)提交数据(GET-POST-AsyncHttpClient)
- Android-向服务器提交数据(GET-POST-AsyncHttpClient)
- android之向服务器提交数据(GET,POST,AsyncHttpClient)
- Android 向服务器提交数据(GET-POST-AsyncHttpClient)
- Android 向服务器提交数据(GET-POST-AsyncHttpClient)
- Android之向服务器提交数据(POST,GET,AsyncHttpClient)
- android 向服务器提交数据(get-post-AsyncHttpClient)
- Android 客户端通过内置API(HttpClient) 访问 服务器(用Spring MVC 架构) 返回的json数据全过程
- 13.Android提交数据到服务器HttpCl…
- HttpClient使用post方式模拟表单提交数据到服务器并下载服务器文件
- android, httpclient,get,发送数据到服务器
- Android HttpClient框架get和post方式提交数据(非原创)
- Android - 向服务器发送数据(POST) - HTTPClient.
- android httpclient获取服务器上面的数据
- HttpClient下载图片和向服务器提交数据实例
- Android -- 提交数据到服务器,Get Post方式, 异步Http框架提交
- HttpClient使用post方式模拟表单提交数据到服务器并下载服务器文件
- android重新学_网络3——采用HttpURLConnection提交数据到服务器
- android get或post及HttpClient与服务器数据交互