大家好日子。请帮我。我的android应用程序总是转到我的CATCH部分。
但是,如果我调用我的.net网络服务,我得到了正确的结果,这里是:
<LoginModel xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://tempuri.org/">
<Status>Accept</Status>
<ErrMessage/>
<ErrNo>1</ErrNo>
<Username>dan</Username>
<Firstname>Dan</Firstname>
<MiddleName>Francisco</MiddleName>
<Lastname>Francisco</Lastname>
<UserID>26d4d0fe-1d39-46c2-bd12-0a1ee4acdc24</UserID>
<UserType>Admin</UserType>
<LastLoginDate>12/6/2014 11:10:58 AM</LastLoginDate>
</LoginModel>
这是我的代码。
package com.example.sampulan;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import org.ksoap2.*;
import org.ksoap2.serialization.*;
import org.ksoap2.transport.*;
public class login extends Activity {
private static final String SOAP_ACTION = "http://tempuri.org/Login";
private static final String OPERATION = "Login";
private static final String NAMESPACE = "http://tempuri.org/";
private static final String URL = "http://10.0.2.2:1338/WSTrafficTicketing.asmx";
Button loginBtn;
String user, pass,errmess,errno,status;
EditText uname, password;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname = (EditText) findViewById(R.id.unameET);
password = (EditText) findViewById(R.id.passET);
loginBtn = (Button) findViewById(R.id.loginB);
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
user = uname.getText().toString();
pass = password.getText().toString();
SoapObject request = new SoapObject(NAMESPACE, OPERATION);
request.addProperty("Username", user);
request.addProperty("Password", pass);
SoapSerializationEnvelope envelop;
envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelop.dotNet = true;
envelop.setOutputSoapObject(request);
HttpTransportSE trans = new HttpTransportSE(URL);
try {
trans.call(SOAP_ACTION,envelop);
SoapObject result = (SoapObject) envelop.getResponse();
SoapObject result2 = (SoapObject) result.getProperty(0);
status = result2.getProperty(0).toString();
if(status.equals("Accept")){
Intent i = new Intent("com.example.sampulan.MENU");
startActivity(i);
}
} catch (Exception e) {
// TODO: handle exception
Intent i = new Intent("com.example.sampulan.LOGIN");
startActivity(i);
}
}
});
}
}
请帮我解决这个问题:(我的代码出了什么问题?
答案 0 :(得分:0)
你无法在UI线程上进行网络操作 试试这个
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
uname = (EditText) findViewById(R.id.unameET);
password = (EditText) findViewById(R.id.passET);
loginBtn = (Button) findViewById(R.id.loginB);
loginBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
user = uname.getText().toString();
pass = password.getText().toString();
new Thread(new Runnable(){
@Override
public void run() {
// TODO Auto-generated method stub
SoapObject request = new SoapObject(NAMESPACE, OPERATION);
request.addProperty("Username", user);
request.addProperty("Password", pass);
SoapSerializationEnvelope envelop;
envelop = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelop.dotNet = true;
envelop.setOutputSoapObject(request);
HttpTransportSE trans = new HttpTransportSE(URL);
try {
trans.call(SOAP_ACTION,envelop);
SoapObject result = (SoapObject) envelop.getResponse();
SoapObject result2 = (SoapObject) result.getProperty(0);
status = result2.getProperty(0).toString();
if(status.equals("Accept")){
Intent i = new Intent("com.example.sampulan.MENU");
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
startActivity(i);
}
});
}
} catch (Exception e) {
// TODO: handle exception
Intent i = new Intent("com.example.sampulan.LOGIN");
runOnUiThread(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
startActivity(i);
}
});
}
}
}).start();
}
});
}