Android的中http协议HttpURLConnection中post请求
来源:互联网 发布:mac 体验 编辑:程序博客网 时间:2024/05/16 14:59
public class MainActivity extends AppCompatActivity {
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.mytext);
//异步任务;
MyTask task=new MyTask();
task.execute("");
}
class MyTask extends AsyncTask<String,Integer,String>{
@Override
protected String doInBackground(String... params) {
//网络请求
return getPostData();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//json解析
Gson gson=new Gson();
DataBean bean=gson.fromJson(s,DataBean.class);
String text=bean.result.data.get(0).imtro;
//更新UI操作
textView.setText(text);
}
}
//通过post请求来从网络读取数据;
private String getPostData(){
String path="http://apis.juhe.cn/cook/query.php";
try {
String value="menu="+ URLEncoder.encode("白菜","utf-8")+"&key=13af589c334ec80c037688e927407966&rn=1";
//post的URL不要加参数值;
URL url=new URL(path);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
//设置请求方式为post;默认是get;
connection.setRequestMethod("POST");
//设置输出数据
connection.setDoOutput(true);
//获取一个输出流,用来设置参数
OutputStream outputStream=connection.getOutputStream();
//将参数放入输出流中;
outputStream.write(value.getBytes());
//获取连接状态码
int code=connection.getResponseCode();
if(code==200){
//获取请求返回的数据
InputStream inputStream=connection.getInputStream();
//将输入流转换为string
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String result="";
String str=null;
while((str=reader.readLine())!=null){
result+=str;
}
return result;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=(TextView)findViewById(R.id.mytext);
//异步任务;
MyTask task=new MyTask();
task.execute("");
}
class MyTask extends AsyncTask<String,Integer,String>{
@Override
protected String doInBackground(String... params) {
//网络请求
return getPostData();
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
//json解析
Gson gson=new Gson();
DataBean bean=gson.fromJson(s,DataBean.class);
String text=bean.result.data.get(0).imtro;
//更新UI操作
textView.setText(text);
}
}
//通过post请求来从网络读取数据;
private String getPostData(){
String path="http://apis.juhe.cn/cook/query.php";
try {
String value="menu="+ URLEncoder.encode("白菜","utf-8")+"&key=13af589c334ec80c037688e927407966&rn=1";
//post的URL不要加参数值;
URL url=new URL(path);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
//设置请求方式为post;默认是get;
connection.setRequestMethod("POST");
//设置输出数据
connection.setDoOutput(true);
//获取一个输出流,用来设置参数
OutputStream outputStream=connection.getOutputStream();
//将参数放入输出流中;
outputStream.write(value.getBytes());
//获取连接状态码
int code=connection.getResponseCode();
if(code==200){
//获取请求返回的数据
InputStream inputStream=connection.getInputStream();
//将输入流转换为string
BufferedReader reader=new BufferedReader(new InputStreamReader(inputStream));
String result="";
String str=null;
while((str=reader.readLine())!=null){
result+=str;
}
return result;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
不要忘了加权限
阅读全文
1 0
- Android的中http协议HttpURLConnection中post请求
- http协议中,get请求和post请求的区别
- Android中HttpURLConnection实现HTTP请求
- Android中利用HttpURLConnection完成Get和Post请求
- Android中利用HttpURLConnection发送Post请求并添加参数的写法
- android中Post方式发送HTTP请求
- Android中HTTP Post和Get请求
- Android中httpURLconnection-post+get
- Java 中使用HttpURLConnection发起POST 请求
- Java中使用HttpURLConnection发起POST 请求
- Android中使用HttpURLConnection和HttpClient发送Http请求
- http协议的post请求
- Android HttpURLConnection post请求
- HTTP协议中POST、GET、HEAD、PUT等请求方法
- HTTP协议中POST、GET、HEAD、PUT等请求方法
- web笔记三:Http协议中get和post请求
- HttpURLConnection的POST请求
- HttpUrlConnection的post请求
- 在Node.js中使用MySQL&MySQL JavaScript客户端
- Linux之用户管理
- Hadoop基础教程-第11章 Hive:SQL on Hadoop(11.3 Hive 快速入门)
- 浏览器 返回状态码汇总
- Mysql5.6 从零开始学 读书笔记
- Android的中http协议HttpURLConnection中post请求
- HDU6078Wavel Sequence(计数dp)
- Qt5构建出错问题解决办法
- D
- HDU-2256
- HOJ13832 Fence
- ITOO---“秒杀”选课之Redis序列化
- XYNUOJ 第四次比赛 The Intervals
- 聊天室入门实战(node,socket.io实现)--第一章(实现登录群聊功能)