HttpUrlConnection和HttpClient和android-async-http框架的GET和POST请求
来源:互联网 发布:贵阳IBM预测大数据 编辑:程序博客网 时间:2024/05/01 05:29
1.HttpUrlConnection
public class NetUtil {public static String doGet(String username,String password) {try {String data = "username=" + username + "&password=" + password;URL url = new URL("http://115.192.188.146:9090/Android//servlet/LoginServlet?"+data);HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("GET");conn.setConnectTimeout(3000);conn.setReadTimeout(5000);if(conn.getResponseCode()==200){InputStream is = conn.getInputStream();String result = getStringFromInputStream(is);return result;}} catch (Exception e) {e.printStackTrace();}return null;}public static String doPost(String username,String password) {try {// post请求的参数String data = "username=" + username + "&password=" + password;URL url = new URL("http://115.192.188.146:9090/Android//servlet/LoginServlet");HttpURLConnection conn = (HttpURLConnection) url.openConnection();conn.setRequestMethod("POST");conn.setConnectTimeout(3000);conn.setReadTimeout(5000);conn.setDoOutput(true);//默认情况下, 系统不允许向服务器输出内容// 获得一个输出流, 用于向服务器写数据, 默认情况下, 系统不允许向服务器输出内容 所以要设置conn.setDoOutput(true);OutputStream os = conn.getOutputStream();os.write(data.getBytes());//将请求的数据写入输出流里os.flush();os.close();if(conn.getResponseCode()==200){InputStream is = conn.getInputStream();String result = getStringFromInputStream(is);return result;}} catch (Exception e) {e.printStackTrace();}return null;}private static String getStringFromInputStream(InputStream is) {try {if(is!=null){int len = 0;byte[] buffer = new byte[1024];ByteArrayOutputStream baos = new ByteArrayOutputStream();while((len=is.read(buffer))!=-1){baos.write(buffer, 0, len);}String result = baos.toString();is.close();baos.close();return result;}} catch (Exception e) {e.printStackTrace();}return null;}}
2.HttpClient
public class NetUtil {public static String doHttpClientGet(String username,String password) {HttpClient client = null;try {String data = "username=" + username + "&password=" + password;client = new DefaultHttpClient();HttpGet get = new HttpGet("http://10.0.2.2:9090/Android//servlet/LoginServlet?"+data);HttpResponse response = client.execute(get);if(response.getStatusLine().getStatusCode()==200){InputStream is = response.getEntity().getContent();return getStringFromInputStream(is);}} catch (Exception e) {e.printStackTrace();} finally {if(client!=null){//关闭连接client.getConnectionManager().shutdown();}}return null;}public static String doHttpClientPost(String username,String password) {HttpClient client = null;try {client = new DefaultHttpClient();HttpPost post = new HttpPost("http://10.0.2.2:9090/Android//servlet/LoginServlet");//设置请求的参数List<NameValuePair> parameters = new ArrayList<NameValuePair>();parameters.add(new BasicNameValuePair("username", username));parameters.add(new BasicNameValuePair("password", password));//把post请求的参数包装了一层UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters);//设置参数post.setEntity(entity);HttpResponse response = client.execute(post);if(response.getStatusLine().getStatusCode()==200){InputStream is = response.getEntity().getContent();return getStringFromInputStream(is);}} catch (Exception e) {e.printStackTrace();}finally{if(client!=null){client.getConnectionManager().shutdown();}}return null;}private static String getStringFromInputStream(InputStream is) {try {if(is!=null){int len = 0;byte[] buffer = new byte[1024];ByteArrayOutputStream baos = new ByteArrayOutputStream();while((len=is.read(buffer))!=-1){baos.write(buffer, 0, len);}String result = baos.toString();is.close();baos.close();return result;}} catch (Exception e) {e.printStackTrace();}return null;}}
3.android-async-http
public class MainActivity extends Activity {private EditText et_username;private EditText et_password;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);init();}private void init() {et_username = (EditText) findViewById(R.id.et_username);et_password = (EditText) findViewById(R.id.et_password);}public void loginGET(View view){String username = et_username.getText().toString();String password = et_password.getText().toString();String data = "username=" + username + "&password=" + password;String url = "http://10.0.2.2:9090/Android//servlet/LoginServlet?"+data;AsyncHttpClient client = new AsyncHttpClient();client.get(url, new MyResponseHandler());//get请求}public void loginPOST(View view){String username = et_username.getText().toString();String password = et_password.getText().toString();String url = "http://10.0.2.2:9090/Android//servlet/LoginServlet";AsyncHttpClient client = new AsyncHttpClient();RequestParams params = new RequestParams();params.put("username", username);params.put("password", password);client.post(url, params , new MyResponseHandler());//post请求}private class MyResponseHandler extends AsyncHttpResponseHandler{//请求成功时调用@Overridepublic void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {//这个回调方法是在主线程执行的,所以在这个方法里可以直接操作UI组件Toast.makeText(getApplicationContext(), "成功:"+statusCode+" body--->"+new String(responseBody), Toast.LENGTH_SHORT).show();}//请求失败时调用@Overridepublic void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {Toast.makeText(getApplicationContext(), "失败:"+statusCode, Toast.LENGTH_SHORT).show();}}}
1 0
- HttpUrlConnection和HttpClient和android-async-http框架的GET和POST请求
- httpurlconnection 和 httpclient 发送get、post请求
- httpclient的post和get,httpurlconnection的post和get
- HttpUrlConnection的GET请求和POST请求
- httpurlconnection的Get请求和Post请求
- HttpURLConnection和Httpclient get请求
- Android-----HttpURLConnection的 get 请求和 post 请求
- HttpClient模拟HTTP的GET和POST请求
- Httpclient的Get请求和Post请求
- HttpClient的Get请求和Post请求
- Android中使用HttpURLConnection和HttpClient实现GET和POST请求访问网络
- android(21)(使用HttpURLConnection,HttpClient和AsyncHttpClient分别实现post和get请求数据)
- HttpUrlConnection post和get请求
- HttpURLconnection/get和post请求
- HttpURLConnection的get和post请求
- HttpURLConnection的GET和POST请求
- HttpURLConnection的get和post请求实例
- HttpUrlConnection的get和post请求方法
- Android SDK Manager 超时不翻墙的解决办法
- hdoj 母牛的故事
- java程序性能优化---2--2.1常有设计模式
- android AtionBar
- 【spoj8757】Kmp 概率Dp
- HttpUrlConnection和HttpClient和android-async-http框架的GET和POST请求
- 在mac上出错:No working C compiler found.
- Android 下拉刷新(刷新布局需用ScrollView包裹)
- VBScript 文件操作
- poj 2491 Scavenger Hunt 字符串离散化
- MovieClip.moveTo 方法
- bind变量的长度不同导致多个chlid cursor
- android之listView缓存机制
- sql 视图 按where条件多个字段取一个