0

Hi I created a webservice that pulls a list of location names from the database in a array. I need to get this array in a listview on android. I am lost as I am new to android. Please help. Here is the code I have so far. Also I am using a fragment and webservice using soap. Thanks in advance.

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 Answer 1

1

Check this code for getting response from web service as array

call your web service in doinbackground() method assign response to list. When interacting with UI do that in op postexecute() method.

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
}
});
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.