package com.example.test___1; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; import org.xmlpull.v1.XmlPullParser; import org.xmlpull.v1.XmlPullParserException; import com.example.test___1.XListView.IXListViewListener; import com.lidroid.xutils.HttpUtils; import com.lidroid.xutils.exception.HttpException; import com.lidroid.xutils.http.ResponseInfo; import com.lidroid.xutils.http.callback.RequestCallBack; import com.lidroid.xutils.http.client.HttpRequest.HttpMethod; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.util.Xml; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class Fragment1 extends Fragment { int index = 1; String url = "http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=" + index + "&pageSize=15"; List<News> newsList = new ArrayList<News>(); Handler handler = new Handler(){ private News news; private String name; private MyBaseAdapter myBaseAdapter; public void handleMessage(android.os.Message msg) { List<News> list = new ArrayList<News>(); String xml = (String) msg.obj; int tag = msg.arg1; XmlPullParser pullParser = Xml.newPullParser(); // 鎶奡tring鏁版嵁杞垚娴� ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( xml.getBytes()); try { pullParser .setInput(new InputStreamReader(byteArrayInputStream)); int eventType = pullParser.getEventType(); // 濡傛灉涓嶆槸缁撴潫鏂囨。灏辩户缁В鏋� while (eventType != XmlPullParser.END_DOCUMENT) { switch (eventType) { // 璇诲埌寮�濮嬫爣绛� case XmlPullParser.START_TAG: // 鑾峰緱鏍囩鍚� name = pullParser.getName(); if ("news".equals(name)) { news = new News(); } else if ("title".equals(name)) { // 寰楀埌title鏍囩閲岀殑鏂囨湰 news.title = pullParser.nextText(); } else if ("body".equals(name)) { news.body = pullParser.nextText(); } else if ("commentCount".equals(name)) { news.commentCount = pullParser.nextText(); } else if ("author".equals(name)) { news.author = pullParser.nextText(); } else if ("pubDate".equals(name)) { news.pubDate = pullParser.nextText(); } break; // 璇诲埌缁撴潫鏍囩 case XmlPullParser.END_TAG: name = pullParser.getName(); if ("news".equals(name)) { list.add(news); } break; } eventType = pullParser.next(); } } catch (XmlPullParserException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } switch (tag) { case 1: newsList.addAll(list); myBaseAdapter = new MyBaseAdapter(getActivity(), newsList); xlistView.setAdapter(myBaseAdapter); break; // 鍒锋柊 case 2: newsList.clear(); newsList.addAll(list); myBaseAdapter.notifyDataSetChanged(); break; // 鍔犺浇鏇村 case 3: newsList.addAll(list); myBaseAdapter.notifyDataSetChanged(); break; } }; }; private XListView xlistView; @Override public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = View.inflate(getActivity(), R.layout.fragment, null); xlistView = (XListView) view.findViewById(R.id.xlistView); xlistView.setPullLoadEnable(true); xlistView.setXListViewListener(new IXListViewListener() { @Override public void onRefresh() { index += 1; getData("http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=" + index + "&pageSize=15", 2); // 鍋滄鍒锋柊 xlistView.stopRefresh(); } @Override public void onLoadMore() { index += 1; getData("http://www.oschina.net/action/api/news_list?catalog=1&pageIndex=" + index + "&pageSize=15", 3); // 鍋滄鍔犺浇鏇村 xlistView.stopLoadMore(); } }); return view; } @Override public void onActivityCreated(@Nullable Bundle savedInstanceState) { getData(url, 1); super.onActivityCreated(savedInstanceState); } private void getData(String url, final int tag) { HttpUtils httpUtils = new HttpUtils(); httpUtils.configCurrentHttpCacheExpiry(0); httpUtils.send(HttpMethod.GET, url, new RequestCallBack<String>() { @Override public void onFailure(HttpException arg0, String arg1) { // TODO Auto-generated method stub } @Override public void onSuccess(ResponseInfo<String> arg0) { // 寰楀埌鏁版嵁xml String result = arg0.result; // 缁檋andler鍙戦�佹暟鎹� Message msg = Message.obtain(); msg.obj = result; msg.arg1 = tag; handler.sendMessage(msg); } }); } }
package com.example.day04; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.List; import com.example.day04.adapter.Adapter; import com.example.day04.bean.Bean; import com.example.day04.bean.Bean.Myresult.Data; import com.google.gson.Gson; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.app.Activity; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.ListView; public class MainActivity extends Activity { private EditText ed; private Button btn; private ListView lv; Handler handler = new Handler() { public void handleMessage(android.os.Message msg) { switch (msg.what) { case 1: String json = (String) msg.obj; Gson gson = new Gson(); Bean bean = gson.fromJson(json, Bean.class); List<Data> list = bean.result.data; System.out.println(list + "------------------------"); lv.setAdapter(new Adapter(list, MainActivity.this)); break; case 2: System.out.println("请求失败"); default: break; } }; }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Init(); } public void Init() { ed = (EditText) findViewById(R.id.mname); btn = (Button) findViewById(R.id.button1); lv = (ListView) findViewById(R.id.lv); btn.setOnClickListener(listener); } OnClickListener listener = new OnClickListener() { @Override public void onClick(View v) { new Thread() { public void run() { String name = "http://apis.juhe.cn/cook/query.php"; try { String path = "?menu=" + URLEncoder.encode(ed.getText().toString(), "utf-8") + "&dtype=&pn=1&rn=5&albums=&=&key=f2158617de65461d79db8b1a597726d5"; URL url = new URL(name + path); HttpURLConnection connection = (HttpURLConnection) url .openConnection(); int code = connection.getResponseCode(); if (code == 200) { InputStream is = connection.getInputStream(); ByteArrayOutputStream array = new ByteArrayOutputStream(); byte[] by = new byte[1024]; int length = 0; while ((length = is.read(by)) != -1) { array.write(by, 0, length); } String str = array.toString("utf-8"); System.out.println(str + "----------"); Message mess = Message.obtain(); mess.obj = str; mess.what = 1; handler.sendMessage(mess); } else { Message mess = Message.obtain(); mess.what = 2; handler.sendMessage(mess); } } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (MalformedURLException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } }; }.start(); } }; }
相关推荐
HttpUtils
httpUtils封装上传图片api
HttpUtils网络请求工具类,可以获取网络上的数据转化给JSON数据然后处理成自己的想要的数据 http://blog.csdn.net/qq_30552993/article/details/51774509
HttpUtils工具类
各种工具类的集合,如 httpUtils/viewUtils等
xUtils之HttpUtils的get和Post请求 xUtils_2.6.4.jar
最全httpClient工具类 .
自己写的基于Java自带URL请求 处理get,post 不明白的,里面直接运行可以跑,
该demo是仿照okhttp请求框架写的。里面包含Basic认证,包含get/post请求。包含重试机制。
使用java代码的方式发出http请求并且转成json对象转成pojo对象,发送http请求,并且尝试重试请求等功能。 支持restful形式的请求方式。
xutil开源库中,的httpUtils是进行网络请求的,可以用来学习下
打造一个通用的网络请求引擎HttpUtils 博客地址:http://blog.csdn.net/jsonnan/article/details/72809815
HTTP 源代码库,包含上传文件,下载文件,批量上传和下载
java发送https请求封装
此为调用http请求的java工具类,基于springframework的RestTemplate,有注释
自己平时用的 Http 请求工具类,以此记录备用,啦啦啦啦啦啦啦凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数凑字数
下面小编就为大家带来一篇HttpUtils 发送http请求工具类(实例讲解)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
http传送,用于客户端从服务器端下载文档等,实现前台后台之间的交互
网络爬虫,模拟浏览器,经过大量测试,这个工具比较好用