轉載,不清楚原文出處,不貼連結了
public class SocketClient { public boolean mClientFlag = false; private static Socket client; public SocketClient() { Log.d("WineStock", "WineStock SocketClient()"); client = new Socket(); } public SocketClient(String HostName, int iPort) { client=new Socket(); SocketAddress socketAddress = new InetSocketAddress(HostName, iPort); try { Log.d("WineStock", "WineStock SocketClient connect"); client.connect(socketAddress, 3000); } catch (IOException e) { // TODO Auto-generated catch block Log.d("WineStock", "WineStock SocketClient IOException"); } catch (IllegalArgumentException e1) { // TODO: handle exception Log.d("WineStock", "WineStock SocketClient IllegalArgumentException "); } return ; } public boolean SocketConnect(String HostName, int iPort) { SocketAddress socketAddress = new InetSocketAddress(HostName, iPort); try { Log.d("WineStock", "WineStock SocketConnect connect "); client.connect(socketAddress, 3000); } catch (IOException e) { // TODO Auto-generated catch block Log.d("WineStock", "WineStock SocketConnect IOException "); } catch (IllegalArgumentException e1) { // TODO: handle exception Log.d("WineStock", "WineStock SocketConnect IllegalArgumentException "); } return true; } public void SocketClose() { try { client.close(); } catch (IOException e) { // TODO Auto-generated catch block Log.d("WineStock", "socket close error"+e.getMessage()); } } // 建立串連 public static boolean SClient(SocketSendModel SendModel) throws UnknownHostException, IOException { boolean bRet = false; try { if(client.isConnected()) { //發送請求 sendMessage(SendModel); bRet = true; } } catch (UnknownHostException e) { Log.d("WineStock", "socket SClienterror"+e.getMessage()); } catch (IOException e1) { Log.d("WineStock", "socket SClienterror"+e.getMessage()); } return bRet; } // 發送請求 private static void sendMessage(SocketSendModel model) throws IOException { //boolean bRet = false; try { PrintWriter out = new PrintWriter(client.getOutputStream()); out.print(objToJsonString(model).replace("\n", " ")); out.flush(); //bRet = true; } catch (IOException e) { } return; } // 接收伺服器資料 public static String readMessage() throws IOException { String str = ""; try { BufferedReader br = new BufferedReader(new InputStreamReader(client.getInputStream())); str = br.readLine().replace("{", "").replace("}", "").replace("\"", ""); } catch ( IOException e) { } return str; } // 將Java Object對象轉換為Json對象 private static String objToJsonString(Object obj) { // 初始化傳回值 String json = "str_empty"; if (obj == null) { return json; } StringBuilder buff = new StringBuilder(); Field fields[] = obj.getClass().getFields(); try { buff.append("["); buff.append("{"); int i = 0; for (Field field : fields) { if (i != 0) { buff.append(","); } buff.append(field.getName()); buff.append(":"); buff.append("\""); buff.append(field.get(obj) == null ? "" : field.get(obj)); buff.append("\""); i++; } buff.append("}"); buff.append("]"); json = buff.toString(); } catch (Exception e) { throw new RuntimeException("cause:" + e.toString()); } return json; }}