Java 在webservice中获取字符串中的每一列,并在android的textview中显示它

Java 在webservice中获取字符串中的每一列,并在android的textview中显示它,java,android,sql,web-services,android-studio,Java,Android,Sql,Web Services,Android Studio,我有一个Web服务: [WebMethod] public string findUserNameById(int Id) { return getStudent(Id); } public String getStudent(int id) { SqlConnection conn; conn = Class1.ConnectionManager.GetConnection();

我有一个Web服务:

   [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(结果);
}
});
}
}

我想要的是,在文本视图中查看每一列。如何在web服务和java代码中做到这一点?我不希望所有数据都是单个字符串,我希望id在字符串中,名称在其他字符串中等等。我应该怎么做?

使用来自web服务的JSON响应并在android中解析JSON

由于android和您的Web服务器是用JAVA编写的,所以您可以使用它来帮助您将对象转换为json,反之亦然

编辑1:

这是一个入门示例

你能参考一个教程吗?编辑了答案,请检查一下