public void postRequest(View view) {
new Thread(new Runnable() {
@Override
public void run() {
OutputStream outputStream = null;
InputStream inputStream = null;
try {
URL url = new URL(BASE_URL+"/post/comment");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(10000);
connection.setRequestProperty("Content-Type","application/json;charset=UTF-8");
CommandItem commandItem = new CommandItem("2341","mark...");
Gson gson = new Gson();
String json = gson.toJson(commandItem);
byte[] bytes = json.getBytes("UTF-8");
Log.e(TAG, "run: "+bytes.length );
connection.setRequestProperty("Content-Length",String.valueOf(bytes.length));
//连接
connection.connect();
//数据给出去
outputStream = connection.getOutputStream();
outputStream.write(bytes);
outputStream.flush();
//拿到结果
int responseCode = connection.getResponseCode();
Log.e(TAG, "run: "+responseCode );
if (responseCode == 200){
inputStream = connection.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream));
String line = br.readLine();
Log.e(TAG, "run: "+line );
}
} catch (Exception e) {
e.printStackTrace();
}finally {
if (outputStream!=null) {
try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (inputStream!=null) {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
}).start();
}
Http的post举栗请求
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- HTTP超文本传输协议,是短连接,是客户端主动发送请求,服务器做出响应,服务器响应之后,链接断开。HTTP是一个属...
- POST /path HTTP/1.1 Content-Type: application/x-www-form-...
- 一、http初识(一)HTTP协议简介超文本传输协议(英文:Hyper Text Transfer Protoco...
- 今天呢,给大家脑补一下,GET和POST在请求时的区别,要说它们的区别呢,接触过WEB端开发的人都能说出一二。 你...