Java 从webservice获取多行并在android listview中显示
我有一个Web服务:Java 从webservice获取多行并在android listview中显示,java,android,sql,web-services,Java,Android,Sql,Web Services,我有一个Web服务: [WebMethod] public string findUserNameById(int Id) { return getStudent(Id); } public String getStudent(int id) { SqlConnection conn; conn = Class1.ConnectionManager.GetConnection();
[WebMethod]
public string findUserNameById(int Id)
{
return getStudent(Id);
}
public String getStudent(int id)
{
SqlConnection conn;
conn = Class1.ConnectionManager.GetConnection();
conn.Open();
SqlCommand newCmd = conn.CreateCommand();
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "select * from dbo.tblUser where Id=" + id + "";
SqlDataReader sdr = newCmd.ExecuteReader();
String address = null;
if (sdr.Read())
{
address = sdr.GetValue(0).ToString();
address += "," + sdr.GetValue(1).ToString();
address += "," + sdr.GetValue(2).ToString();
}
conn.Close();
return address;
}
它检索如下行值:Id、name、grade。我从android应用程序中调用此Web服务:
public class MainActivity extends AppCompatActivity {
private EditText editText;
private TextView textView;
private Handler mHandler= new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
textView = (TextView)findViewById(R.id.textView);
}
public void getName(View v){
String inputId =editText.getText().toString();
//String[] params= new String[]{"10.0.2.2",inputId};
String[] params= new String[]{"192.168.1.17:90",inputId};
new MyAsyncTask().execute(params);
}
class MyAsyncTask extends AsyncTask<String, Void, String>
{
public String SOAP_ACTION="http://tempuri.org/findUserNameById";
public String OPERATION_NAME ="findUserNameById";
public String WSDL_TARGET_NAMESPACE ="http://tempuri.org/";
public String SOAP_ADDRESS;
private SoapObject request;
private HttpTransportSE httpTransport;
private SoapSerializationEnvelope envelop;
Object response= null;
@Override
protected String doInBackground(String... params) {
SOAP_ADDRESS="http://"+params[0]+"/myWebService.asmx";
request= new SoapObject(WSDL_TARGET_NAMESPACE,OPERATION_NAME);
PropertyInfo pi=new PropertyInfo();
pi.setName("Id");
pi.setValue(Integer.parseInt(params[1]));
pi.setType(Integer.class);
request.addProperty(pi);
pi= new PropertyInfo();
envelop= new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelop.dotNet=true;
envelop.setOutputSoapObject(request);
httpTransport=new HttpTransportSE(SOAP_ADDRESS);
try{
httpTransport.call(SOAP_ACTION,envelop);
response=envelop.getResponse();
}
catch (Exception e){
response=e.getMessage();
}
return response.toString();
}
@Override
protected void onPostExecute(final String result){
super.onPostExecute(result);
mHandler.post(new Runnable() {
@Override
public void run() {
textView.setText(result);
}
});
}
}
public类MainActivity扩展了AppCompatActivity{
私人编辑文本;
私有文本视图文本视图;
私有处理程序mHandler=新处理程序();
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText=(editText)findViewById(R.id.editText);
textView=(textView)findViewById(R.id.textView);
}
public void getName(视图v){
字符串inputId=editText.getText().toString();
//字符串[]参数=新字符串[]{“10.0.2.2”,inputId};
字符串[]参数=新字符串[]{“192.168.1.17:90”,inputId};
新建MyAsyncTask().execute(参数);
}
类MyAsyncTask扩展了AsyncTask
{
公共字符串SOAP_ACTION=”http://tempuri.org/findUserNameById";
公共字符串操作\u NAME=“findUserNameById”;
公共字符串WSDL_目标_命名空间=”http://tempuri.org/";
公共字符串地址;
私有对象请求;
私人httpTransport-httpTransport;
私人信封;
对象响应=null;
@凌驾
受保护的字符串doInBackground(字符串…参数){
SOAP_ADDRESS=“http://”+params[0]+“/myWebService.asmx”;
请求=新的SoapObject(WSDL\u目标\u命名空间、操作\u名称);
PropertyInfo pi=新的PropertyInfo();
pi.setName(“Id”);
pi.setValue(Integer.parseInt(params[1]);
pi.setType(Integer.class);
请求。添加属性(pi);
pi=新属性info();
信封=新的SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet=true;
Envelope.setOutputSoapObject(请求);
httpTransport=新的HttpTransportSE(SOAP\U地址);
试一试{
调用(SOAP_操作,信封);
response=envelope.getResponse();
}
捕获(例外e){
response=e.getMessage();
}
返回response.toString();
}
@凌驾
受保护的void onPostExecute(最终字符串结果){
super.onPostExecute(结果);
mHandler.post(新Runnable(){
@凌驾
公开募捐{
setText(结果);
}
});
}
}
我想获取所有行并在android中的列表视图中显示它,怎么做
查询类似于:select*from dbo.tblUser
我应该在Web服务中更改什么?在java for android中我应该做什么?试试下面的代码-
SoapObject response = (SoapObject) envelope.getResponse();
yourArray=new String[response.getPropertyCount()];
for(int i=0;i<response.getPropertyCount();i++){
Object property = response.getProperty(i);
if(property instanceof SoapObject){
SoapObject final_object = (SoapObject) property;
yourArray[i] = final_object.getProperty("YOUR_PROPERTY_NAME");
}
}
SoapObject response=(SoapObject)envelope.getResponse();
yourArray=新字符串[response.getPropertyCount()];
对于(inti=0;i我建议您创建一个模型类,该类保存您需要发送回的所有属性
Public class Address{
public int grade;
public String name;
public String grade;
}
Create List<Address> addressList;
换成
List<Address> addressList=new ArrayList<Address>();
while (sdr.Read())
{
Address address=new Address();
address.id = sdr.GetValue(0);
address.name= sdr.GetValue(1).ToString();
address.grade=sdr.GetValue(2).ToString();
addressList.add(address);
}
return addressList;
List addressList=new ArrayList();
while(sdr.Read())
{
地址=新地址();
address.id=sdr.GetValue(0);
address.name=sdr.GetValue(1.ToString();
address.grade=sdr.GetValue(2.ToString();
地址列表。添加(地址);
}
返回地址列表;
声明一个POJO-
class AllocatedData{
String Id, name, grade;
getters and constructor
}
代码-
List<AllocatedData> list = new ArrayList<AllocatedData>();
if (responseLevel4 != null) {
for(int i = 0; i < responseLevel4.getPropertyCount(); i++){
responseLevel5 = (SoapObject) responseLevel4.getProperty(i);
Data allocated = new AllocatedData(checkStringProperty("Id"),checkStringProperty("name"),
checkStringProperty("grade"));
list.add(allocated);
}
}
}
但我的建议是使用JSON响应或在JSON中生成响应。
我以前在Asp.net web服务中尝试过,在其中很难生成JSON。请尝试WCF服务
链接:-
通过使用json,您可以通过GSON liabrary直接解析,这与SOAP兼容,速度非常快
Gson Gson=新的Gson();
对于(int i=0;i }您自己的代码有什么问题?@sud我的代码很好,但它只获取一行并在android的textview中显示。我想要的是获取多行并在android的listview中显示它。首先,我应该在webservice代码中更改什么以使其返回数组?尝试在MainActivity类中声明您的数组是否可以编写Web服务代码和java代码?您使用哪种语言编写Web服务?java还是C?我使用C。在address类中,我声明了getter和setter?而且我没有得到这行代码:Create List addressList;您能发布整个address类和整个Web服务方法吗?因为我是新手,尝试过这样做,我得到了一个很多错误。所以你能发布它让我理解吗?请看这篇文章,因为我不是c#方面的专家。它有一个很好的例子,正是你想要的。我想使用JSON,但我没有一个完整的很好的教程,你能参考一个吗?你可以参考这个url。我以前用过它
List<AllocatedData> list = new ArrayList<AllocatedData>();
if (responseLevel4 != null) {
for(int i = 0; i < responseLevel4.getPropertyCount(); i++){
responseLevel5 = (SoapObject) responseLevel4.getProperty(i);
Data allocated = new AllocatedData(checkStringProperty("Id"),checkStringProperty("name"),
checkStringProperty("grade"));
list.add(allocated);
}
}
}
public String checkStringProperty(String propertyName){
if(responseLevel5.hasProperty(propertyName)){
return responseLevel5.getProperty(propertyName).toString();
} else {
return null;
}
}