Phần xử lý sự kiện nút:
Login_btn.setOnClickListener((View v) ->
{
byte[] login_info = new byte[1000];
FileInputStream fIn;
try {
fIn = new FileInputStream(f);
fIn.read(login_info);
fIn.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
String str = new String(login_info);
login_info = null;
int index = str.indexOf('-');
String s1 = str.substring(0, index);
String s2 = str.substring(index + 1);
List<Parameter> parameters = new ArrayList<>();
parameters.add(new Parameter("user_name", s1));
parameters.add(new Parameter("pass", s2));
SoapObject that_user =
Connector.Send_request( "Login", null, null, parameters);
});
Hàm gọi ksoap(Connector):
lass MyRunnable implements Runnable
{
public static final String NAME_SPACE = "http://tempuri.org/";
public static final String URL = "https://192.168.1.176:44383/Messenger_service.asmx";
private SoapObject obj;
private String Class_name, method_name;
private Class class_obj;
private List<Parameter> parameters;
public MyRunnable(String method_name, String Class_name, Class class_obj,
List<Parameter> parameters)
{
this.Class_name = Class_name;
this.class_obj = class_obj;
this.method_name = method_name;
this.parameters = parameters;
}
@Override
public void run()
{
SoapObject request = new SoapObject(NAME_SPACE, method_name);
for(Parameter par : parameters)
request.addProperty(par.getName(), par.getObj());
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(request);
if(Class_name != null)
{
envelope.addMapping(NAME_SPACE, Class_name, class_obj);
}
HttpTransportSE transporter = new HttpTransportSE(URL);
transporter.debug = true;
try
{
transporter.call(NAME_SPACE + method_name, envelope);
obj = (SoapObject) envelope.getResponse();
} catch (IOException e) {
e.printStackTrace();
} catch (XmlPullParserException e) {
e.printStackTrace();
}
}
public SoapObject getObj()
{
return obj;
}
}
public class Connector
{
public static final String NAME_SPACE = "http://tempuri.org/";
public static SoapObject Send_request(String method_name, String Class_name, Class class_obj,
List<Parameter> parameters)
{
MyRunnable r = new MyRunnable(method_name, Class_name, class_obj, parameters);
Thread t = new Thread(r);
t.start();
try {
t.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
return r.getObj();
}
}
Nó báo lỗi trên không hiểu vì sao ạ
Lạ lùng một chỗ là khi thế s1, s2 bằng string literal thì nó connect được với web service. Còn để s1, s2 thì nó báo lỗi. @@
Phần logcat của em:
Process: com.example.messenger, PID: 6942
java.lang.NullPointerException: Attempt to invoke virtual method ‘int java.io.InputStream.read(byte[], int, int)’ on a null object reference
at org.ksoap2.transport.HttpTransportSE.readDebug(HttpTransportSE.java:320)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:271)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:118)
at org.ksoap2.transport.HttpTransportSE.call(HttpTransportSE.java:113)
at com.example.messenger.Connector.MyRunnable.run(Connector.java:62)