Skip to content

Commit

Permalink
V1.3.7-10:优化demo;修复soap的异常捕获
Browse files Browse the repository at this point in the history
  • Loading branch information
peterzhen40 committed Nov 21, 2018
1 parent d9922c7 commit 2c5db92
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 63 deletions.
7 changes: 7 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,11 @@ dependencies {
implementation 'com.android.support:appcompat-v7:26.0.2'
testImplementation 'junit:junit:4.12'
implementation project(path: ':soap')
//配置 RxJava
implementation 'io.reactivex.rxjava2:rxjava:2.2.0'
implementation 'io.reactivex.rxjava2:rxandroid:2.0.2'
implementation 'com.google.code.gson:gson:2.8.2'
implementation 'com.google.code.ksoap2-android:ksoap2-android-assembly:3.6.2'
implementation 'com.squareup.okhttp3:okhttp:3.8.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
}
4 changes: 2 additions & 2 deletions app/src/main/java/zhaoyang/soap/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ public static ILoginService createLogin() {
// .readTimeout(20 * 10000L, TimeUnit.MILLISECONDS)
// .build();
Soap soap = new Soap.Builder()
.baseUrl("http://192.168.11.109:7001")
.baseUrl(MainActivity.BASE_URL)
.timeout(20*1000)
.callFactory(AndroidCallFactory.create())
.build();
Expand All @@ -25,7 +25,7 @@ public static ILoginService createLogin() {

public static ICommonService commonService() {
Soap soap = new Soap.Builder()
.baseUrl("http://192.168.11.109:7001")
.baseUrl(MainActivity.BASE_URL)
.timeout(20*1000)
.callFactory(AndroidCallFactory.create())
.build();
Expand Down
84 changes: 63 additions & 21 deletions app/src/main/java/zhaoyang/soap/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,54 +2,96 @@

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.zyy.soap.Soap;

import org.reactivestreams.Subscriber;
import org.reactivestreams.Subscription;

import retrofit2.Retrofit;

public class MainActivity extends AppCompatActivity {

private static final String TAG = "MainActivity";
public static String BASE_URL = "http://192.168.11.109:7001";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Soap.setDEBUG(BuildConfig.DEBUG);
setContentView(R.layout.activity_main);
final TextView textView = findViewById(R.id.textView);
final EditText etAddress = findViewById(R.id.et_address);
final EditText etUsername = findViewById(R.id.et_username);
final EditText etPassword = findViewById(R.id.et_password);
etAddress.setText(BASE_URL);
etUsername.setText("test");
etPassword.setText("88888");

findViewById(R.id.button).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Api.commonService()
.login("slj", "88888", "1").subscribeWith(new Subscriber<UserInfo>() {
@Override
public void onSubscribe(Subscription s) {
Log.d(TAG, "onSubscribe: ");
s.request(Long.MAX_VALUE);
}
String address = etAddress.getText().toString();
String username = etUsername.getText().toString();
String password = etPassword.getText().toString();

@Override
public void onNext(UserInfo userInfo) {
textView.setText(userInfo.toString());
Log.d(TAG, "onNext: " + userInfo.toString());
}
if (!TextUtils.isEmpty(address) && isValidUrl(address)) {
BASE_URL = address;
if ( !TextUtils.isEmpty(username) && !TextUtils.isEmpty(password)) {
Api.commonService().login(username, password, "1")
.subscribe(new Subscriber<UserInfo>() {
@Override
public void onSubscribe(Subscription s) {
Log.d(TAG, "onSubscribe: ");
s.request(Long.MAX_VALUE);
}

@Override
public void onError(Throwable t) {
textView.setText(t.getMessage());
Log.d(TAG, "onError: ", t);
}
@Override
public void onNext(UserInfo userInfo) {
textView.setText(userInfo.toString());
Log.d(TAG, "onNext: " + userInfo.toString());
}

@Override
public void onComplete() {
Log.d(TAG, "onComplete: ");
@Override
public void onError(Throwable t) {
textView.setText(t.getMessage());
Log.d(TAG, "onError: ", t);
}

@Override
public void onComplete() {
Log.d(TAG, "onComplete: ");
}
});
}
});

}

}
});
}

public static boolean isValidUrl(String url) {
if (TextUtils.isEmpty(url)) {
return false;
}
if (Patterns.WEB_URL.matcher(url).matches()) {
try {
new Retrofit.Builder()
.baseUrl(url)
.build();
return true;
} catch (Exception e) {
e.printStackTrace();
return false;
}
}
return false;
}
}
23 changes: 21 additions & 2 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
Expand All @@ -9,8 +9,27 @@
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="zhaoyang.soap.MainActivity">

<EditText
android:id="@+id/et_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="输入地址"/>

<EditText
android:id="@+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="用户名"/>

<EditText
android:id="@+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="密码"/>

<Button
android:id="@+id/button"
android:layout_width="wrap_content"
Expand All @@ -32,4 +51,4 @@
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/button" />
</RelativeLayout>
</LinearLayout>
4 changes: 2 additions & 2 deletions soap/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 15
targetSdkVersion 22
versionCode 9
versionName "1.3.6"
versionCode 10
versionName "1.3.7"
}
buildTypes {
release {
Expand Down
1 change: 1 addition & 0 deletions soap/src/main/java/com/zyy/soap/Soap.java
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ public void subscribe(@NonNull FlowableEmitter emitter) throws Exception {
}
}
} catch (Exception e) {
//在此捕获异常
if (!emitter.isCancelled()) {
emitter.onError(e);
}
Expand Down
77 changes: 41 additions & 36 deletions soap/src/main/java/com/zyy/soap/improved/RxService.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,50 +72,55 @@ public static String call(String namespace, String endPoint, String methodName,
}
}

/**
* 不在此捕获错误,直接抛出
* @param namespace
* @param endPoint
* @param methodName
* @param params
* @param isNewSystem
* @param timeout
* @return
* @throws Exception
*/
public static String call(String namespace, String endPoint, String methodName,
Map<String, Object> params, boolean isNewSystem, int timeout) {
Map<String, Object> params, boolean isNewSystem, int timeout) throws Exception {

try {
SoapPrimitive resultObject = null;
// 指定WebService的命名空间和调用的方法名
SoapObject request = new SoapObject(namespace, methodName);
SoapPrimitive resultObject = null;
// 指定WebService的命名空间和调用的方法名
SoapObject request = new SoapObject(namespace, methodName);

// 设置需调用WebService接口需要传入的两个参数
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, Object> e : params.entrySet()) {
if (e.getValue() instanceof byte[]) {
byte[] d = (byte[]) e.getValue();
String data = Base64.encode(d);
request.addProperty(e.getKey(), data);
} else {
request.addProperty(e.getKey(), e.getValue());
}
// 设置需调用WebService接口需要传入的两个参数
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, Object> e : params.entrySet()) {
if (e.getValue() instanceof byte[]) {
byte[] d = (byte[]) e.getValue();
String data = Base64.encode(d);
request.addProperty(e.getKey(), data);
} else {
request.addProperty(e.getKey(), e.getValue());
}
}
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;
}
// 生成调用WebService方法的SOAP请求信息,并指定SOAP的版本
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.bodyOut = request;

// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = false;
// 设置是否调用的是dotNet开发的WebService
envelope.dotNet = false;

HttpTransportSE ht = new HttpTransportSE(endPoint, timeout);
if (!isNewSystem) {
ht.call(namespace + methodName, envelope);
} else {
ht.call(null, envelope);
}
if (envelope.getResponse() != null) {
resultObject = (SoapPrimitive) envelope.getResponse();
}
String resultStr = (null == resultObject ? "" : resultObject.toString());
RxLog.log(namespace, endPoint, methodName, params, resultStr);
return resultStr;
} catch (Exception e) {
String returnStr = e.getMessage();
RxLog.log(namespace, endPoint, methodName, params, returnStr, e);
return returnStr;
HttpTransportSE ht = new HttpTransportSE(endPoint, timeout);
if (!isNewSystem) {
ht.call(namespace + methodName, envelope);
} else {
ht.call(null, envelope);
}
if (envelope.getResponse() != null) {
resultObject = (SoapPrimitive) envelope.getResponse();
}
String resultStr = (null == resultObject ? "" : resultObject.toString());
RxLog.log(namespace, endPoint, methodName, params, resultStr);
return resultStr;
}

}

0 comments on commit 2c5db92

Please sign in to comment.