Okhttp request data get and post in Android

Source: Internet
Author: User

1: Add and then sync in Android Studio under Build.gradle

' com.squareup.okhttp:okhttp:2.4.0 '
' com.squareup.okio:okio:1.5.0 '
' junit:junit:4.12 '

such as: yellow part
Dependencies {
Compile Filetree (Include: [' *.jar '],Dir' Libs ')
Testcompile' junit:junit:4.12 '
Compile' com.android.support:appcompat-v7:23.4.0 '

Compile' com.github.bumptech.glide:glide:3.6.1 '
Compile files (' Libs/gson-2.2.4.jar ')

Compile ' com.squareup.okhttp:okhttp:2.4.0 '
Compile ' com.squareup.okio:okio:1.5.0 '
Compile ' junit:junit:4.12 '
}

2:get Request
Such as:
/**
* Okhttpclient GET request
*/
Create a Okhttpclient object
Okhttpclient mokhttpclient =New Okhttpclient ();
Create a request
Final Request Request =New Request.builder ()
. URL ("Http://m.yunifang.com/yunifang/mobile/goods/getall?random=97575&encode=f1c61556b05c0cf193e74b1e8fc6420c &category_id=17 ")
. build ();
New Call
Call call = Mokhttpclient.newcall (request);
Request Join Schedule
Call.enqueue (New Callback ()
{
@Override
public void OnFailure (Request request, IOException e)
{
}
@Override
public void Onresponse (final Response Response) throws IOException
{
String htmlstr = Response.body (). String ();
Log. i (
}
});

3:post Request Note : Some interfaces do not support post requests
Such as:
/**
* OKHttp POST request
*/
Create a Okhttpclient object
Okhttpclient mokhttpclient =New Okhttpclient ();
Formencodingbuilder Builder =New Formencodingbuilder ();
Builder.add ("Random","97575");
Builder.add ("Encode","f1c61556b05c0cf193e74b1e8fc6420c");
Builder.add ("Category_id","17");


Create a request
http://www.yulin520.com/a2a/impressApi/news/mergeList?sign=C7548DE604BCB8A17592EFB9006F9265&pageSize=10 &gender=2&ts=1871746850&page=1
http://m.yunifang.com/yunifang/mobile/goods/getall?random=97575&encode=f1c61556b05c0cf193e74b1e8fc6420c &category_id=17
Final Request Request =New Request.builder ()
. URL ("Http://m.yunifang.com/yunifang/mobile/goods/getall")
. Post (Builder.build ()). build ();
New Call
Call call = Mokhttpclient.newcall (request);
Request Join Schedule
Call.enqueue (New Callback ()
{
@Override
public void OnFailure (Request request, IOException e)
{
Toast.maketext (Mainactivity.this, "request Failed", Toast.length_short). Show ();

}

@Override
public void Onresponse (Final Response Response)Throws IOException
{
String htmlstr = Response.body (). String ();
Log.I"Htmlstr---", htmlstr);

Gson gson=new Gson ();
Gods Gods=gson.fromjson (htmlstr,gods. Class);
databean=gods.getdata ();
if (adapter==null) {
adapter=new myadapter (databean,mainactivity.this);
gv.setadapter ( adapter);
}
adapter.notifydatasetchanged ();
}
});
               
         

Okhttp request data get and post in Android

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.