Skip to content

Commit

Permalink
ip地址更新用Constants添加,写的不好,不一遍遍提代码了...
Browse files Browse the repository at this point in the history
  • Loading branch information
liwenxing3028 committed Apr 12, 2018
1 parent 7fdb7d9 commit 3ea8200
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 15 deletions.
2 changes: 2 additions & 0 deletions app/src/main/java/com/dydl/electronicdemo/BaseApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ public void onCreate() {
XLog.init(/*BuildConfig.DEBUG ? */LogLevel.ALL /*: LogLevel.NONE*/
, new LogConfiguration.Builder().b().build());



}

public static Context getContext() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.dydl.electronicdemo.module.ExamBean;
import com.dydl.socketlib.OkSocket;
import com.dydl.socketlib.callback.OkCallBack;
import com.dydl.socketlib.common.Constants;
import com.dydl.socketlib.helper.OkParams;
import com.dydl.socketlib.utils.DateFormatUtils;

Expand All @@ -18,6 +19,7 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

Constants.setServerIp("192.168.0.148");

OkParams okParams = new OkParams();
okParams.put("1", "ZN232")
Expand All @@ -44,6 +46,8 @@ public void onSuccess(ExamBean result) {
}
);



OkSocket.sendMsgString(
okParams,//上传数据
6000,//端口号
Expand Down
2 changes: 2 additions & 0 deletions socketlib/src/main/java/com/dydl/socketlib/OkSocket.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,10 @@ public class OkSocket {




protected OkSocket(Context context) {
mContext = context.getApplicationContext(); //获取Application的Context

}

//OkSocket单例对象
Expand Down
21 changes: 16 additions & 5 deletions socketlib/src/main/java/com/dydl/socketlib/common/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,22 @@
public class Constants {


public static final String PATTERNCODER = "(?<!\\d)\\d{4}(?!\\d)";
public static final String SERVER_IP = "ip";
public static final String SERVER_PORT = "port";
public static final String NAME = "name";
public static final String PWD = "password";
private static String SERVER_IP = "127.0.0.1";
private static String SERVER_PORT = "8080";

public static String getServerIp() {
return SERVER_IP;
}

public static void setServerIp(String serverIp) {
SERVER_IP = serverIp;
}

public static String getServerPort() {
return SERVER_PORT;
}

public static void setServerPort(String serverPort) {
SERVER_PORT = serverPort;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -106,13 +106,9 @@ public static Observable<OkStrBean> createSocket(OkParams bean, int port) {

private static Observable<OkStrBean> createIpObservable(String sendStr, int port) {
return Observable.create(subscriber -> {
String url = null;
if (SharePUtils.isContains(Constants.SERVER_IP)) {
url = SharePUtils.getString(Constants.SERVER_IP);
}
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(url, port), 10000);
socket.connect(new InetSocketAddress(Constants.getServerIp(), port), 10000);
PrintWriter writer = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream(), "UTF-8")));
writer.print(sendStr);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,9 @@ public static Observable<OkStrBean> createResultXml(OkStrBean okBean) {

public static Observable<OkStrBean> createIpObservable(String sendStr, int port) {
return Observable.create(subscriber -> {
String url = null;
if (SharePUtils.isContains(Constants.SERVER_IP)) {
url = SharePUtils.getString(Constants.SERVER_IP);
}
try {
Socket socket = new Socket();
socket.connect(new InetSocketAddress(url, port), 10000);
socket.connect(new InetSocketAddress(Constants.getServerIp(), port), 10000);
PrintWriter writer = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream(), "UTF-8")));
writer.print(sendStr);
Expand Down

0 comments on commit 3ea8200

Please sign in to comment.