First suggest a moduel, first write a download image code:
public class LoadTest extends asynctask<void,void,byte[]>{
public static LoadTest LoadTest;
Private byte[] data;
Private String URL;
Public Setdatal listence;
public void Setdatalistence (Setdatal l) {
This.listence = l;
}
Public byte[] Getload () {
return data;
}
public void SetUrl (String s) {
This.url = s;
}
@Override
Protected byte[] Doinbackground (Void ... params) {
try {
Bytearrayoutputstream outputstream = new Bytearrayoutputstream ();
URL urlimage = new URL (URL);
HttpURLConnection urlconn = (httpurlconnection) urlimage.openconnection ();
Urlconn.setconnecttimeout (5000);
Urlconn.setrequestmethod ("GET");
int sumlenght = Urlconn.getcontentlength ();
InputStream Errorstream = Urlconn.geterrorstream ();
int code = Urlconn.getresponsecode ();
if (HTTPURLCONNECTION.HTTP_OK = = code) {
InputStream is = Urlconn.getinputstream ();
byte[] buff = new byte[512];
int Len;
while (len = is.read (buff))! =-1) {
Outputstream.write (Buff,0,len);
}
Is.close ();
Outputstream.flush ();
data = Outputstream.tobytearray ();
}
} catch (Malformedurlexception e) {
E.printstacktrace ();
} catch (IOException e) {
E.printstacktrace ();
}
return data;
}
@Override
protected void OnPostExecute (byte[] datainfo) {
if (null! = Datainfo) {
Listence.getdata (Datainfo);
}else {
Super.onpostexecute (Datainfo);
}
}
public interface setdatal{
void GetData (byte[] data);
}
}
Open Moduel's Gradle:
Write code:
Task Makejar (type:copy) {
Delete ' Build/libs/myjar.jar '
From (' build/intermediates/bundles/release/')
Into (' build/libs/')
Include (' Classes.jar ')
Rename (' Classes.jar ', ' Myjar.jar ')
}
executing the build jar package at the terminal
./gradlew Build
Build.finalizedby Makejar
or
Makejar.dependson (build)
./gradlew Makejar
Open Terminal Terminal Input command: Gradview Makejar
The generated jar is in the \build\intermediates\bundles\release directory. You can see a Java flag
This is OK, the import of other projects can be used a few
Demo:https://github.com/tiaodong/okhttptest
Test Demo:https://github.com/tiaodong/jartest
Android simple hit Jar pack