问题
How can we fetch Data from ksoap web service, show in Android Autocomplete textview search suggestion, using Onclick Search button.
回答1:
EditText not showing suggestion.
AutoCompleteTextView provides suggestion in editText.
First you need to parse data from server. Then make adapter with that data and set in AutoCompleteTextView.
For more information I suggest you to check this blog
And for your reference also check this link to parse ksoap Webservice.
回答2:
Working code For Autocomplete textview data Fetch from ksoap webservice, Using Search icon Click Request .
it Will Show the Suggestion from Ksoap WebService.
package com.example;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONObject;
import org.ksoap2.SoapEnvelope;
import org.ksoap2.serialization.PropertyInfo;
import org.ksoap2.serialization.SoapObject;
import org.ksoap2.serialization.SoapPrimitive;
import org.ksoap2.serialization.SoapSerializationEnvelope;
import org.ksoap2.transport.HttpTransportSE;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.LabeledIntent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.text.Html;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class FindCity extends ActionBarActivity implementsOnItemClickListener, OnClickListener
{
AutoCompleteTextView SearchAutoComplte;
Button searchicon;
// for WebService
private static final String SOAP_ACTION2 = "http:*************";
private static final String NAMESPACE2 = "http:********************";
private static final String URL2 = "http:****************";
private static final String METHOD_NAME2 = "**********";
private String TAG2 = "City";
public static String Status_Response = "";
public String Autocomplete_SearchValue;
public String Cityname;
String[] CITYNAME;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SearchAutoComplte=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
Autocomplete_SearchValue =SearchAutoComplte.getText().toString();
searchicon = (Button) findViewById(R.id.Search_iconimage);
searchicon.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Search_iconimage:
AsyncCallWS task = new AsyncCallWS();
// Call execute
task.execute();
break;
}
}
private class AsyncCallWS extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
Log.i(TAG2, "doInBackground");
try {
getdata(Autocomplete_SearchValue);
} catch (Exception e) {
Toast.makeText(getApplicationContext(),"error caught in do in background", Toast.LENGTH_SHORT).show();
e.printStackTrace();
}
return null;
// return null;
}
@Override
protected void onPostExecute(Void result) {
Log.i(TAG2, "onPostExecute");
try {
ArrayAdapter<String> adapter = new ArrayAdapter<String>(FindCity.this,android.R.layout.select_dialog_singlechoice, CITYNAME);
//SearchEdittext.setThreshold(1);
SearchAutoComplte.setAdapter(adapter);
} catch (Exception e) {
Log.i(TAG2, "Error");
e.printStackTrace();
}
}
@Override
protected void onPreExecute() {
Log.i(TAG2, "onPreExecute");
}
@Override
protected void onProgressUpdate(Void... values) {
Log.i(TAG2, "onProgressUpdate");
}
}
public void getdata(String SearchValue)
{
// Create request
SoapObject request = new SoapObject(NAMESPACE2, METHOD_NAME2);
PropertyInfo pi4 = new PropertyInfo();
pi4.setName("City");
pi4.setValue(SearchValue);// get the string that is to be sent to the webservice
pi4.setType(String.class);
request.addProperty(pi4);
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL2);
try {
// Invole web service
androidHttpTransport.call(SOAP_ACTION2, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
//Converting string to Array list
ArrayList<String> Servciecityname_arr= new ArrayList<String>();
if ((response.toString()).contains("{"))
{
SoapObject rep = (SoapObject) envelope.bodyIn;
JSONArray jr = new JSONArray(rep.getPropertyAsString(0));
for (int i = 0; i < jr.length(); i++) {
JSONObject jb = (JSONObject) jr.get(i);
Cityname = jb.getString("CityName123");
Servciecityname_arr.add(Cityname);
}
CITYNAME = new String[Servciecityname_arr.size()];
CITYNAME = Servciecityname_arr.toArray(CITYNAME);
}
else
{
Status_Response = response.toString();
}
} catch (Exception e) {
Log.i(TAG2, "Error in catch");
e.printStackTrace();
}
}
}
回答3:
public void getSignupdata(String SearchValue, String CityLocation)
{
// Create request
SoapObject request = new SoapObject(NAMESPACE2, METHOD_NAME2);
PropertyInfo pi3 = new PropertyInfo();
pi3.setName("search");
pi3.setValue(SearchValue);// get the string that is to be sent to the webservice
pi3.setType(String.class);
request.addProperty(pi3);
PropertyInfo pi4 = new PropertyInfo();
pi4.setName("City");
pi4.setValue(CityLocation);// get the string that is to be sent to the webservice
pi4.setType(String.class);
request.addProperty(pi4);
// Create envelope
SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(SoapEnvelope.VER11);
envelope.dotNet = true;
// Set output SOAP object
envelope.setOutputSoapObject(request);
// Create HTTP call object
HttpTransportSE androidHttpTransport = new HttpTransportSE(URL2);
try {
// Invole web service
androidHttpTransport.call(SOAP_ACTION2, envelope);
// Get the response
SoapPrimitive response = (SoapPrimitive) envelope.getResponse();
if ((response.toString()).contains("{"))
{
// JSONArray jr = new JSONArray(response);
SoapObject rep = (SoapObject) envelope.bodyIn;
JSONArray jr = new JSONArray(rep.getPropertyAsString(0));
for (int i = 0; i < jr.length(); i++) {
JSONObject jb = (JSONObject) jr.get(i);
ServiceCenterName = jb.getString("CenterName");
String strArray[] = ServiceCenterName.split(" ");
System.out.println("String converted to String array");
//print elements of String array
for(int i=0; i < strArray.length; i++)
{
System.out.println(strArray[i]);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_singlechoice, strArray);
SearchEdittext.setThreshold(1);
SearchEdittext.setAdapter(adapter)
}
}
else
{
Status_Response_FindStores_Landing = response.toString();
}
} catch (Exception e) {
Log.i(TAG2, "Error in catch");
e.printStackTrace();
}
}
来源:https://stackoverflow.com/questions/29406833/autocomplete-textview-data-fetch-from-ksoap-webservice-using-search-icon-onclic