列表视图中的Android Web服务返回数组

时间:2014-02-10 20:22:53

标签: android xml web-services soap

您好我创建了一个Web服务,它从数组中的数据库中提取位置名称列表。我需要在android的listview中获取这个数组。我迷失了,因为我是新手。请帮忙。这是我到目前为止的代码。我也在使用片段和使用肥皂的web服务。提前谢谢。

public class LocationFragment extends Fragment {
/** Called when the activity is first created. */

TextView tv;

public LocationFragment(){}
   private String TAG ="Vik";

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_location, container, false);
    tv =(TextView)rootView.findViewById(R.id.textView1);
    AsyncCallWS task = new AsyncCallWS();
    task.execute();
    return rootView;
}
private class AsyncCallWS extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        Log.i(TAG, "doInBackground");
        location();
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        Log.i(TAG, "onPostExecute");
    }

    @Override
    protected void onPreExecute() {
        Log.i(TAG, "onPreExecute");
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        Log.i(TAG, "onProgressUpdate");
    }

}       


 public void location() 
    {
        String SOAP_ACTION = "http://example.com/locations";
        String METHOD_NAME = "locations";
        String NAMESPACE = "http://example.com/";
        String URL = "http://100.100.00.80/example/Service.asmx";   

        try { 
            SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
            Request.addProperty("getlocation", "null");

            SoapSerializationEnvelope soapEnvelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
            soapEnvelope.dotNet = true;
            soapEnvelope.setOutputSoapObject(Request);

            HttpTransportSE transport= new HttpTransportSE(URL);

            transport.call(SOAP_ACTION, soapEnvelope);
            SoapPrimitive resultString = (SoapPrimitive)soapEnvelope.getResponse();
            tv.setText("Status : " + resultString);
            Log.i(TAG, "Result locations: " + resultString);
        }
        catch(Exception ex) {
            Log.e(TAG, "Error: " + ex.getMessage());
        }


    }

}

1 个答案:

答案 0 :(得分:1)

检查此代码以获取来自Web服务的响应作为数组

在doinbackground()方法中调用您的Web服务,将响应分配给列表。与UI交互时,在op postexecute()方法中执行此操作。

class addassdist extends AsyncTask<Void, Void, Void> {

private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);

private final String SOAP_ACTION = "http://tempuri.org/GetDistrict";
private final String METHOD_NAME = "GetDistrict";
private final String NAMESPACE = "http://tempuri.org/";
private final String URL = "http://192.160.1.1/district/service.asmx";

@Override
protected void onPreExecute() {
this.dialog.setMessage("Loading data");
this.dialog.show();
}

@Override
protected Void doInBackground(Void... unused) {

SoapObject Request = new SoapObject(NAMESPACE, METHOD_NAME);
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
envelope.setOutputSoapObject(Request);
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);

try {
androidHttpTransport.call(SOAP_ACTION, envelope);
SoapObject response = (SoapObject) envelope.getResponse();

System.out.println("response"+response);
int intPropertyCount = response.getPropertyCount();
list= new String[intPropertyCount];

for (int i = 0; i < intPropertyCount; i++)
{               
list[i] = response.getPropertyAsString(i).toString();
}
}

catch (Exception e) {
exc=true;
e.printStackTrace();

}
return null;
}


@Override
protected void onPostExecute(Void result) {


if (this.dialog.isShowing()) {
this.dialog.dismiss();
}       
if(exc)
{
Toast.makeText(MainActivity.this,"Error" , Toast.LENGTH_LONG).show();
}
else{
spinner();
exc=false;
}
}
}



public void spinner(){

Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);

ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, list);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);

spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onNothingSelected(AdapterView<?> parent) {

}

@Override
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
// your code
}
});
}