//乱码
intent.putExtra("bks.name",URLEncoder.encode(bks.name,"utf-8"));
//HttpClient 请求
private void initData() throws Exception {
HttpClient client = new DefaultHttpClient();
HttpGet get = new HttpGet(path);
HttpResponse response = client.execute(get);
if (response.getStatusLine().getStatusCode() == 200) {
//httpclinet 对请求数据格式编码
String result = EntityUtils.toString(response.getEntity(), "GBK");
Gson gson = new Gson();
Data data = gson.fromJson(result, Data.class);
Message msg = Message.obtain();
msg.obj = data.data;
handler.sendMessage(msg);
}
}
============== HttpURLConnection 请求=====json=======================
public void getData( String path) throws Exception {
//创建出url的对像
URL url = new URL(path);
//根据url对象创建开始连接的对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置它的请求方式
connection.setRequestMethod("GET");
//设置它的连接超时时间
connection.setConnectTimeout(5000);
//设置它的读取超时时间
connection.setReadTimeout(5000);
//得到它的请求码
int responseCode = connection.getResponseCode();
if(responseCode==200){
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] arr = new byte[1024];
int len = 0;
while((len=is.read(arr))!=-1){
baos.write(arr, 0, len);
}
//服务器给客户端返回来的结果
final String result = baos.toString();
Gson gson = new Gson();
Data data = gson.fromJson(result, Data.class);
Message msg = Message.obtain();
msg.obj = data.data;
handler.sendMessage(msg);
}
}
get 请求(java)
、、、、、、、、、、、、、、、、
new Thread(){
public void run() {
try {
//创建出url的对像
URL url = new URL(path);
//根据url对象创建开始连接的对象
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
//设置它的请求方式
connection.setRequestMethod("GET");
//设置它的连接超时时间
connection.setConnectTimeout(5000);
//设置它的读取超时时间
connection.setReadTimeout(5000);
//得到它的请求码
int responseCode = connection.getResponseCode();
if(responseCode==200){
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] arr = new byte[1024];
int len = 0;
while((len=is.read(arr))!=-1){
baos.write(arr, 0, len);
}
//服务器给客户端返回来的结果
final String result = baos.toString();
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, result, 0).show();
}
});
}
} catch (Exception e) {
e.printStackTrace();
}
};
}.start();
''''''''''''''''''''''''''''''''''''
post请求
。。。。。。。。。。。。。。。。。。。。
public class NetWorkUtils {
static String path= "http://169.254.135.196:8080/LogServer/servlet/LogServlet";
public static String getStr(String name,String passWord){
try {
String data = "userName="+URLEncoder.encode(name, "utf-8")+"&passWord="+passWord;
URL url = new URL(path);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.setConnectTimeout(5000);
//如果是true,代表允许客户端向服务器发送数据
connection.setDoOutput(true);
//向服务发送数据
connection.getOutputStream().write(data.getBytes());
int responseCode = connection.getResponseCode();
if(responseCode==200){
InputStream is = connection.getInputStream();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] arr = new byte[1024];
int len = 0;
while((len=is.read(arr))!=-1){
baos.write(arr, 0, len);
}
return baos.toString();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
。。。。。。。。。。。。。。。。。。。。。。
HttpClient get post 请求
------------------------------
public class NewWorkutils {
public static String getstr(String name,String pass){
final String path = "http://169.254.182.202:8081/LogServer/LogServer?username="+name+"&password="+pass;
try {
//创建请求
HttpClient client=(HttpClient) new DefaultHttpClient();
//请求及http请求方式
HttpGet get = new HttpGet(path);
//请求设置请求方式得到请求对象
HttpResponse execute = client.execute(get);
//获得状态栏的状态码
int code = execute.getStatusLine().getStatusCode();
if(code==200){
//请求对象得到请求到的输入流
InputStream is = execute.getEntity().getContent();
ByteArrayOutputStream baos=new ByteArrayOutputStream();
int len=0;
byte[]arr=new byte[1024];
while((len=is.read(arr))!=-1){
baos.write(arr, 0, len);
}
return baos.toString();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
public static String getStr(String username,String password){
String path="http://169.254.182.202:8081/LogServer/LogServer?username="+username+"&password="+password;
try {
//创建httpclint请求
HttpClient client=new DefaultHttpClient();
//请求方式及路径
HttpGet get=new HttpGet(path);
//为请求设设置请求方式
HttpResponse response = client.execute(get);
//
int code = response.getStatusLine().getStatusCode();
if(code==200){
// InputStream is = response.getEntity().getContent();
// ByteArrayOutputStream baos=new ByteArrayOutputStream();
// byte[] arr=new byte[1024];
// int len=0;
// while((len=is.read(arr))!=-1){
// baos.write(arr, 0, len);
// }
// return baos.toString();
//获得结果可以给结果设置格式
return EntityUtils.toString(response.getEntity(),"GBK");
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static String getstrPost(String name,String pass){
try {
final String path = "http://169.254.182.202:8081/LogServer/LogServer";
//创建请求
HttpClient client=(HttpClient) new DefaultHttpClient();
//请求及http请求方式
HttpPost post = new HttpPost(path);
//存储提交值的请求
List<NameValuePair> parameters=new ArrayList<NameValuePair>();
///存值
parameters.add(new BasicNameValuePair("username", name));
parameters.add(new BasicNameValuePair("password", pass));
HttpEntity entity=new UrlEncodedFormEntity(parameters);
//为请求方式携带值
post.setEntity(entity);
//请求设置请求方式得到请求对象
HttpResponse execute = client.execute(post);
//获得状态栏的状态码
int code = execute.getStatusLine().getStatusCode();
Log.e("1111", code+"");
if(code==200){
//提交数据的实体
return EntityUtils.toString(execute.getEntity());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}