ServerSocket server=new ServerSocket(1234);
Socket client=server.accept();
InputStream in = client.getInputStream();
OutputStream ut=client.getOutputStream();
byte bs[] = new byte[1024];
in.read(bs);
String str= new String(bs);
System.out.println(str);
out.write("Server send".getBytes());
out.flush();
client.close();
String str = "client send"; out.write(str.getBytes()); out.flush(); byte bs[] = new byte[1024]; in.read(bs); System.out.println(new String(bs)); server.close();
ServerSocket server=new ServerSocket(1234);
Socket client=server.accept();
BufferedReader in=
new BufferedReader(new InputStreamReader(client.getInputStream()));
PrintWriter ut=new PrintWriter(client.getOutputStream());
String str=in.readLine();
System.out.println(str);
out.println("Server send");
out.flush();Socket server = new Socket("192.168.0.100", 1234);
BufferedReader in = new BufferedReader(new InputStreamReader(
server.getInputStream()));
PrintWriter ut = new PrintWriter(server.getOutputStream());
String str = "client send";
out.println(str);
out.flush();
System.out.println(in.readLine());
server.close();HttpURLConnection默认使用get的方式与后台交互 HttpURLConnection conn = null; try { URL u = new URL("http://192.168.0.100:8080/test.jsp?uuid=123"); conn = (HttpURLConnection)u.openConnection(); BufferedReader br = new BufferedReader(new InputStreamReader(conn.getInputStream())); String line = ""; while((line=br.readLine())!=null){ Log.i("NetTest","lien="+line); } } catch (Exception e) {e.printStackTrace();} finally{ conn.disconnect();}
Thread t =new Thread(new Runnable() {
public void run() {
//上面的代码
}
});
t.start();URL u = new URL("http://192.168.0.100:8080/test.jsp");
conn = (HttpURLConnection)u.openConnection();
//因为这个是post请求,下面两个需要设置为true
conn.setDoOutput(true);
conn.setDoInput(true); // 设置以POST方式
conn.setRequestMethod("POST");
// Post 请求不能使用缓存
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
// 配置本次连接的Content-type,配置为application/x-www-form-urlencoded的
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
//DataOutputStream流
DataOutputStream ut = new DataOutputStream(conn.getOutputStream());
//要上传的参数
String content = "uuid=" + URLEncoder.encode("post测试", "utf-8");
//将要上传的内容写入流中
out.writeBytes(content);
//刷新、关闭
out.flush();
out.close();//封装用于请求的 方法 对象
HttpGet get = new HttpGet("http://192.168.0.100:8080/test.jsp?uuid=uuid121&name=name222");
//创建一个Http的客户端对象
HttpClient client = new DefaultHttpClient();
try{ //发送请求,并获得返回对象
HttpResponse response = client.execute(get);
//从response对象里面把返回值取出来
HttpEntity entity = response.getEntity();
//得到返回内容的流,接下来就是流式操作了
InputStream in = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(in));
StringBuffer buffer = new StringBuffer();
String tempStr = "";
while((tempStr=reader.readLine())!=null){
buffer.append(tempStr);
}
in.close();//应该写finally里面去
reader.close();//应该写finally里面去
Log.i("javass",buffer.toString());
}catch(Exception err){err.printStackTrace();}
//封装用于请求的 方法 对象
HttpPost post = new HttpPost("http://192.168.0.100:8080/test.jsp");
//为post组织参数
NameValuePair uuid = new BasicNameValuePair("uuid","postUuid");
NameValuePair name = new BasicNameValuePair("name","postname");
List list = new ArrayList();
list.add(uuid);
list.add(name);
//把这些参数封装到HttpEntity中
HttpEntity reqEntity = null;
reqEntity = new UrlEncodedFormEntity(list);
//然后把HttpEntity设置到post对象里面去
post.setEntity(reqEntity);
//创建一个Http的客户端对象
HttpClient client = new DefaultHttpClient();
//发送请求,并获得返回对象
HttpResponse response = client.execute(post); JSONObject j = new JSONObject(jsonData); String uuid = j.getString(“uuid");
JSONArray ja = new JSONArray(jsonData); for(int i=0;i
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/26715458/viewspace-717435/ ,如需转载,请注明出处,否则将追究法律责任。