package com.ch.myutils;
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List;
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; package com.ch.myutils;
import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.ArrayList; import java.util.List;
import org.apache.http.HttpEntity; import org.apache.http.HttpResponse; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.entity.UrlEncodedFormEntity; import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpPost; import org.apache.http.impl.client.DefaultHttpClient; import org.apache.http.message.BasicNameValuePair; import org.apache.http.params.BasicHttpParams; import org.apache.http.params.HttpConnectionParams; import org.apache.http.params.HttpParams; import org.apache.http.util.EntityUtils;
import android.content.Context; import android.net.ConnectivityManager; import android.net.NetworkInfo;
public class NetWorkUtil { public static final String LOGIN_URL = "http://101.200.142.201:8080/tqyb/login"; public static boolean isNetAvailable(Context context){ //获得网络管理器 ConnectivityManager connM = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo netInfo = connM.getActiveNetworkInfo();//得到网络详情
if(netInfo == null || !netInfo.isAvailable()) return false;
return true; }
public static String loginCheck_Get_HttpClient(String name,String pass,String url){ //通过StringBuffer来加工url StringBuffer sb = new StringBuffer(url); sb.append("?username="); sb.append(name); sb.append("&userpass="); sb.append(pass);
String result = "";
HttpGet get = new HttpGet(sb.toString());//创建httpClient的get请求对象 //设置请求参数 HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 5*1000); HttpConnectionParams.setSoTimeout(params, 5*1000);
HttpClient client = new DefaultHttpClient(params);//创建HttpClient对象
try { HttpResponse res = client.execute(get);//执行请求,获得结果 if(res.getStatusLine().getStatusCode() == 200){ HttpEntity entity = res.getEntity();//获得相应结果,是一个HttpEntity对象 result = EntityUtils.toString(entity, "utf-8");//转换为字符串 } } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return result;
}
public static String loginCheck_POST_HttpClient(String name,String pass,String url){ String result = "";
HttpPost post = new HttpPost(url);//创建httpClient的post请求对象 //设置请求参数 HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, 5*1000); HttpConnectionParams.setSoTimeout(params, 5*1000); //传值 //创建List集合,存放向服务器传递的数据 List<NameValuePair> reqData = new ArrayList<NameValuePair>(); NameValuePair p1 = new BasicNameValuePair("username", name); NameValuePair p2 = new BasicNameValuePair("userpass", pass); reqData.add(p1); reqData.add(p2);
try { HttpEntity entity = new UrlEncodedFormEntity(reqData, "utf-8"); post.setEntity(entity);//给post请求对象,设置上传的数据 HttpClient client = new DefaultHttpClient(params);//创建HttpClient对象 HttpResponse res = client.execute(post);//执行post请求,获得结果 if(res.getStatusLine().getStatusCode() == 200){ HttpEntity resEntity = res.getEntity();//获得相应结果,是一个HttpEntity对象 result = EntityUtils.toString(resEntity, "utf-8");//转换为字符串 } } catch (UnsupportedEncodingException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); }
return result;
}
}
|