android端向服务器提交请求的几种方式

1、GET方式

其实GET方式说白了,就是拼接字符串。。最后拼成的字符串的格式是: path ?  username= ....& password= ......

public boolean loginByGet(String path, String username , String password) throws Exception{
		
		String url_path = path +"?username=" + URLEncoder.encode(username, "utf-8") + "&password="+password;
		
		URL url = new URL(url_path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		
		conn.setRequestMethod("GET");
		conn.setConnectTimeout(5000);
		if(conn.getResponseCode() == 200){
			return true;
		}
		
		
		return false;
	}


2、POST方式

post方式中,一定要设置以下两个请求参数

conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
conn.setRequestProperty("Content-Length", entity.length + "");


完整代码如下:

public boolean loginByPost(String path,String username , String password) throws Exception{

		System.out.println("LoginService的loginByPost()");
		URL url = new URL(path);
		HttpURLConnection conn = (HttpURLConnection) url.openConnection();
		
		conn.setRequestMethod("POST");
		conn.setConnectTimeout(5000);
		
		String value = "username=" + username +"&" + "password=" +password;
        byte[] entity = value.getBytes();
		 
		conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
		conn.setRequestProperty("Content-Length", entity.length + "");
		
		conn.setDoOutput(true);
		OutputStream os = conn.getOutputStream();
		os.write(entity);
		
		if(conn.getResponseCode() == 200){
			return true;
		}
		
		return false;
	}


3、通过HttpClient以GET方式提交请求

使用HttpClient这个第三方框架以后,我们在编写代码的时候就看不到URL、conn之类的。他其实就是对Http协议进行了封装

public boolean loginByHttpClientGet(String path,String username , String password) throws Exception{
		
		
		String value = path + "?username=" + username +"&password=" + password; 
		HttpClient httpClient =  new DefaultHttpClient();
		HttpGet httpGet = new HttpGet(value);
		
		HttpResponse httpResponse = httpClient.execute(httpGet);
	    if(httpResponse.getStatusLine().getStatusCode() == 200){
	    	return true;
	    }
	    
	    return false;
	}
	



4、通过HttpClient以POST方式提交请求

public boolean loginByHttpClientPost(String path,String username , String password)throws Exception{
		
		HttpClient httpClient = new DefaultHttpClient();
		HttpPost httpPost = new HttpPost(path);
		
		List<NameValuePair> parameters = new ArrayList<NameValuePair>();
		parameters.add(new BasicNameValuePair("username", username));
		parameters.add(new BasicNameValuePair("password", password));
		
		UrlEncodedFormEntity entity = new UrlEncodedFormEntity(parameters,"utf-8");
		httpPost.setEntity(entity);
		HttpResponse httpResponse = httpClient.execute(httpPost);
	    if(httpResponse.getStatusLine().getStatusCode() == 200){
	    	return true;
	    }
	    
	    return false;
	}


  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Android 应用可以通过网络请求来与服务器进行交互。常见的网络请求方式有以下几种: 1. HttpUrlConnection:使用 Java 标准库提供的 HttpUrlConnection 类来建立和服务器的连接,发送请求和接收响应。 2. OkHttp:一个广泛使用的第三方网络请求库,提供了简单易用的 API,支持同步和异步请求,以及文件上传和下载等功能。 3. Retrofit:另一个广泛使用的第三方网络请求库,基于 OkHttp 实现,使用注解方式定义接口和请求参数,提供了强大的请求和响应转换功能。 4. Volley:Google 推出的网络请求库,提供了方便的 API,支持缓存和图片加载等功能,但已不再维护。 Android 应用需要在清单文件中声明网络权限,例如: ```xml <uses-permission android:name="android.permission.INTERNET" /> ``` 在应用代码中,可以使用上述任意一种网络请求方式来进行网络请求,并处理响应结果。例如,使用 HttpUrlConnection 发送 GET 请求并解析响应: ```java URL url = new URL("http://example.com/api/data"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("GET"); conn.connect(); if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) { InputStream inputStream = conn.getInputStream(); BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream)); String line; StringBuilder result = new StringBuilder(); while ((line = bufferedReader.readLine()) != null) { result.append(line); } bufferedReader.close(); inputStream.close(); String responseData = result.toString(); // 解析响应数据 } conn.disconnect(); ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

帅气的东哥

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值