HttpClient的GET和POST请求
来源:互联网 发布:用电脑电视直播软件 编辑:程序博客网 时间:2024/05/17 03:53
public class MainActivity extends AppCompatActivity { private final String GETURL = "http://v.juhe.cn/toutiao/index?type=top&&key=22a108244dbb8d1f49967cd74a0c144d"; private String POSTURL = "http://v.juhe.cn/toutiao/index"; private String response_msg = ""; private List<Bean2> list; private MyAdapter ma; private ListView lv; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); lv = (ListView) findViewById(R.id.lv); list = new ArrayList<Bean2>(); } public void get(View v) { new Thread(new Runnable() { @Override public void run() { String news = getNews(); Jiexi(news); } }).start(); } public void post(View v) { new Thread(new Runnable() { @Override public void run() { postNews("top", "22a108244dbb8d1f49967cd74a0c144d"); } }).start(); } public String getNews() { StringBuffer result = new StringBuffer(); //创建HttpClient对象 打开一个浏览器 HttpClient httpclient = new DefaultHttpClient(); //创建Http对象,get请求的对象, 传入url HttpGet httpGet = new HttpGet(GETURL); try { HttpResponse httpResponse = httpclient.execute(httpGet); //根据响应行,拿到响应码 int responseCode = httpResponse.getStatusLine().getStatusCode(); response_msg = httpResponse.getStatusLine().getReasonPhrase(); if (200 == responseCode) { //得到返回的实体对象 HttpEntity entity = httpResponse.getEntity(); InputStream in = entity.getContent(); BufferedReader reader = new BufferedReader(new InputStreamReader(in)); String line = null; while ((line = reader.readLine()) != null) { result.append(line); } } } catch (Exception e) { e.printStackTrace(); } return result.toString(); } /** * post请求 */ public String postNews(String type, String key) { String result = null; HttpClient client = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(POSTURL); //封装传递参数的集合 List<NameValuePair> parameters = new ArrayList<>(); //添加要传递的参数 parameters.add(new BasicNameValuePair("type", type)); parameters.add(new BasicNameValuePair("key", key)); //封装为实体对象 try { UrlEncodedFormEntity encodedFormEntity = new UrlEncodedFormEntity(parameters, "utf-8"); //把实体对象存入到HttpPost对象中 httpPost.setEntity(encodedFormEntity); //调用第一步中创建好的实例execute方法来执行第二步中创建好的method实例 HttpResponse response = client.execute(httpPost); //根据响应行, 拿到响应码 int responseCode = response.getStatusLine().getStatusCode(); response_msg = response.getStatusLine().getReasonPhrase(); if (200 == responseCode) { HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); ByteArrayOutputStream byteOut = new ByteArrayOutputStream(); int length = 0; byte[] buffer = new byte[1024]; while ((length = in.read(buffer)) != -1) { byteOut.write(buffer, 0, length); } result = byteOut.toString(); } } catch (Exception e) { e.printStackTrace(); } return result; } private void Jiexi(String news) { try { JSONObject obj = new JSONObject(news); JSONObject result = obj.getJSONObject("result"); JSONArray data = result.getJSONArray("data"); if(data!=null&&data.length()>0){ for (int i = 0; i < data.length(); i++) { JSONObject js = data.getJSONObject(i); Bean2 b = new Bean2() ; b.title = js.getString("title"); b.date = js.getString("date"); b.category = js.getString("category"); b.author_name = js.getString("author_name"); list.add(b); } } if(list !=null){ runOnUiThread(new Runnable() { @Override public void run() { setDate(); } }); } } catch (JSONException e) { e.printStackTrace(); } } private void setDate() { ma=new MyAdapter(MainActivity.this,list); lv.setAdapter(ma); }
阅读全文
0 0
- Httpclient的Get请求和Post请求
- HttpClient的Get请求和Post请求
- httpclient的get和post请求
- HttpClient的get和post请求数据
- HttpClient 的post和get请求
- httpClient的Get和Post请求
- HttpClient的post和get请求
- httpclient的get和post请求
- HttpClient的GET和POST请求
- httpClient----Post 和 GET请求
- Httpclient post和get请求
- java使用httpclient封装post请求和get的请求
- HttpClient中的GET请求和POST请求
- HttpClient模拟HTTP的GET和POST请求
- HttpClient 实现get和post请求的过程
- httpClient post/get请求。
- HttpClient get,Post请求
- HttpClient get、post请求
- JBoss与Tomcat的区别
- 数据结构实验之查找一:二叉排序树
- 用JavaScript或python将html中的table导出为excel文件
- python正则表达式
- 《简明Python教程》笔记二
- HttpClient的GET和POST请求
- STORM入门之(集成Redis)
- IOS 新手入门实现第一个HelloWorld项目
- 线程锁Java.util.concurrent.lock与Synchronized区别简述
- 【错误解决】Intellj(IDEA) warning no artifacts configured
- Two strings hdu6170 dp递推
- [自用门户]div居中显示,加在style里面
- Servlet学习
- 高通平台常用缩写