废话少说,直接上代码。简单易懂。
xml如下:
<?xml version="1.0" enCoding="utf-8"?><relativeLayout xmlns:androID="http://schemas.androID.com/apk/res/androID" xmlns:tools="http://schemas.androID.com/tools" androID:ID="@+ID/activity_net" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:orIEntation="vertical" tools:context="com.example.waterlamp.NetActivity"> <button androID:ID="@+ID/okhttp" androID:text="okhttp请求" androID:layout_wIDth="150dp" androID:layout_height="37dp" androID:layout_alignBottom="@+ID/http" androID:layout_alignParentEnd="true" androID:layout_alignParenttop="true" /> <button androID:ID="@+ID/http" androID:text="http请求" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParenttop="true" androID:layout_alignParentStart="true" /> <ScrollVIEw androID:layout_wIDth="match_parent" androID:layout_height="match_parent" androID:layout_alignParentStart="true" androID:layout_below="@+ID/okhttp" androID:layout_alignParentBottom="true" androID:layout_alignParentEnd="true" > <TextVIEw androID:ID="@+ID/stringData" androID:layout_wIDth="match_parent" androID:layout_height="match_parent" /> </ScrollVIEw></relativeLayout>
activity如下:
public class NetActivity extends AppCompatActivity implements VIEw.OnClickListener{private button http,okhttp; private TextVIEw stringData; private String web="https://www.baIDu.com"; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_net); http= (button) findVIEwByID(R.ID.http); okhttp= (button) findVIEwByID(R.ID.okhttp); stringData= (TextVIEw) findVIEwByID(R.ID.stringData); http.setonClickListener(this); okhttp.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { switch (v.getID()){ case R.ID.http: sendRequestWithhttpURLConnection(); break; case R.ID.okhttp: sendRequestWithokhttp(); break; } } private voID sendRequestWithhttpURLConnection(){ new Thread(new Runnable() { @OverrIDe public voID run() { httpURLConnection connection=null; BufferedReader reader=null; try { URL url=new URL(web); connection= (httpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); inputStream in=connection.getinputStream(); reader=new BufferedReader(new inputStreamReader(in)); StringBuffer response=new StringBuffer(); String line; while ((line=reader.readline())!=null){ response.append(line); } showRespond(response.toString()); } catch (MalformedURLException e) { e.printstacktrace(); } catch (IOException e) { e.printstacktrace(); }finally { if (reader!=null){ try { reader.close(); } catch (IOException e) { e.printstacktrace(); } } } } }).start(); } private voID showRespond(final String response) { runOnUiThread(new Runnable() { @OverrIDe public voID run() { stringData.setText(response); } }); } private voID sendRequestWithokhttp(){ new Thread(new Runnable() { @OverrIDe public voID run() { try { OkhttpClIEnt clIEnt=new OkhttpClIEnt(); Request request=new Request.Builder() .url(web).build(); Response response=clIEnt.newCall(request).execute(); String str=response.body().string(); showRespond(str); } catch (IOException e) { e.printstacktrace(); } } }).start(); }}
注意:需要在加权限
1.<uses-permission androID:name="androID.permission.INTERNET"/>
2.okhttp3需要在gradle添加依赖
dependencIEs { compile filetree(dir: 'libs',include: ['*.jar']) androIDTestCompile('com.androID.support.test.espresso:espresso-core:2.2.2',{ exclude group: 'com.androID.support',module: 'support-annotations' }) compile 'com.androID.support:appcompat-v7:24.2.1' compile 'com.androID.support:design:24.2.1' compile 'com.squareup.okhttp3:okhttp:3.4.1'//依赖 testCompile 'junit:junit:4.12'}
以上这篇httpURLConnection和okhttp两种获取网络数据的实现方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持编程小技巧。
您可能感兴趣的文章:Android通过HttpURLConnection和HttpClient接口实现网络编程Android开发使用HttpURLConnection进行网络编程详解【附源码下载】Android中HttpURLConnection与HttpClient的使用与封装Android网络技术HttpURLConnection详解 总结以上是内存溢出为你收集整理的HttpURLConnection和okHttp两种获取网络数据的实现方法全部内容,希望文章能够帮你解决HttpURLConnection和okHttp两种获取网络数据的实现方法所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)