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