Home » Java » Network requests and network requests to download ...

Network requests and network requests to download pictures of the tool class, Android development java tools

package cc.jiusan.www.utils;
Import org.apache.http.HttpEntity;
Import org.apache.http.HttpResponse;
Import org.apache.http.client.HttpClient;
Import org.apache.http.client.methods.HttpGet;
Import org.apache.http.impl.client.DefaultHttpClient;
Import org.apache.http.util.EntityUtils;
Import android.content.Context;
Import android.net.ConnectivityManager;
Import android.net.NetworkInfo;
Public, class, HttpUtils {
/ * *
* to determine whether the current network is connected
*
* @param context
* @return
* /
Public, static, Boolean, isNetWorkConn (Context, context) {
ConnectivityManager Manager = (ConnectivityManager) context
.getSystemService (Context.CONNECTIVITY_SERVICE);
NetworkInfo info = manager.getActiveNetworkInfo ();
If (info = = null) {
Return, info.isConnected ();
}
Return false;
}
/ / network request to download pictures
Public, static, byte[], loadImageFromNet (String, URL) {
//OKHttp
{try
HttpClient client = new, DefaultHttpClient ();
HttpGet getRequest = new HttpGet (URL);
HttpResponse response = client.execute (getRequest);
/ / 500 server error
If (response.getStatusLine) (.GetStatusCode) (= = 200) {
HttpEntity entity = response.getEntity ();
Return EntityUtils.toByteArray (entity);
}
} catch (Exception, e) {
E.printStackTrace ();
}
Return null;
}
}

Latest