Android studio 数据请求注意点

接口传参:

String name = "abc"; ---获取页面输入的内容,可用findViewById获取到;
String password = "123"; ---获取页面输入的内容,可用findViewById获取到;
JSONObject jsonObject = new JSONObject();
jsonObject.put("name",name);
jsonObject.put("pwd",password);
String value = jsonObject.toString(); ---value为传给接口的数据。{"name":"abc","password":"123"}

请求格式:

URL murl = new URL(url);
HttpURLConnection httpURLConnection = (HttpURLConnection) murl.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setRequestProperty("Connection","keep-alive");
httpURLConnection.setRequestProperty("Content-Type","application/json"); ---json格式
httpURLConnection.setRequestProperty("charset","UTF-8");

其他代码网上都有范例,可自行查找。

你可能感兴趣的:(Android studio 数据请求注意点)