Android-okhttp Preliminary use

Source: Internet
Author: User

Since Google has changed the Android request framework to Okhttp, the Android developers have become more and more hot discussion, all of me as a hanging silk Android programmer, also can not be too backward, so take it for yourself, although the current project development is not okhttp, But it's good to improve your development skills by looking ahead.

It is only required to be used at present, without requiring a comprehensive understanding of its principles.

  

First, to use Okhttp, you first import two jar dependency packages. Okhttp.jar (I'm currently using 2.7.0) and Okio.jar (currently 1.6.0) to Libs, and then a burst of crackling build path, it's OK.

This example requests JSON data (which is the JSON data for the weather forecast, which can also be practiced hand with this request when you are all right.) ), as for the parsing of JSON data, do not do, just request down, and then do a show on the line, everything else is very simple. haha haha ....

OK, here's the code directly, which is to set a button in a layout. A TextView, when the button is clicked, requests the data and then displays it on the TextView. (This example requires a second request to be shown on the TextView for a good analysis.) )

Layout layouts:

  

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= "${relativepackage}.${activityclass}" >    <button         android:id= "@+id/bt_ok"        Android:layout_width= "Match_parent"        android:layout_height= "wrap_content"        android:text= "okhttp Request Network"        android:gravity= "Center_horizontal"        android:clickable= "true"        android:onclick= "Btok"        />    <textview        android:id= "@+id/tv_ok"        android:layout_width= "wrap_content"        android:layout_ height= "Wrap_content"        android:layout_below= "@id/bt_ok"        android:gravity= "center_horizontal"        / ></RelativeLayout>

The following is the logical code.

package Com.example.jwwokhttp;import Java.io.ioexception;import Com.squareup.okhttp.okhttpclient;import Com.squareup.okhttp.request;import Com.squareup.okhttp.response;import Android.app.activity;import Android.os.bundle;import Android.view.view;import Android.widget.Button;import Android.widget.textview;public class Okhttpactivity extends Activity {private okhttpclient client;private TextView Tvok ;p rivate Response response;private String responseok; @Overrideprotected void OnCreate (Bundle savedinstancestate) { Super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_okhttp); Initview ();} private void Initview () {//TODO initialize layout Tvok = (TextView) Findviewbyid (R.ID.TV_OK);} public void Btok (View v) throws IOException {new Thread (new Runnable () {
///) because the network request is a time-consuming operation, to open a child thread and put it in a child thread request. @Overridepublic void Run () {Logutil.debug (GetClass (), "Btok:: (::::::::");//TODO Request network Client in thread = new Okhttpclient ();
Here we go, instantiate the object,

String url = "http://www.weather.com.cn/adat/cityinfo/101190404.html";
Request Setup Requests request = new Request.builder (). URL (URL). build (); try {response = Client.newcall (Request). Execute (); if (Response.issuccessful ()) {//returns data Responseok = Response.body (). String (); Logutil.debug (GetClass (), "Network Return Data:::::::::::" +responseok);}} catch (IOException e) {//TODO auto-generated catch Blocke.printstacktrace ();}}). Start (); Tvok.settext (responseok+ "");}}

It's easy to look at. There are many examples on the Internet, here only to do a simple operation, do not let themselves outdated, do not do in-depth study. When you use it, you can learn it better.

Android-okhttp Preliminary use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.