自动完成文本视图数据从 ksoap web 服务获取,使用搜索图标 Onclick 请求和异步任务
Autocomplete textview data Fetch from ksoap webservice, Using Search icon Onclick request with asynchronous task
我们如何从 soap 网络服务中获取数据,显示在 Android Autocompletetextview 搜索建议中,使用 Onclick 搜索按钮。
EditText 未显示建议。
AutoCompleteTextView 在 editText 中提供建议。
首先你需要从服务器解析数据。
然后使用该数据制作适配器并在 AutoCompleteTextView 中设置。
有关更多信息,我建议您查看 this blog
为了您的参考,还要检查 this link 以解析 ksoap Web 服务。
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();
}
}
用于从 ksoap web 服务获取自动完成文本视图数据的工作代码,使用搜索图标单击请求。
它将显示来自 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();
}
}
}
我们如何从 soap 网络服务中获取数据,显示在 Android Autocompletetextview 搜索建议中,使用 Onclick 搜索按钮。
EditText 未显示建议。
AutoCompleteTextView 在 editText 中提供建议。
首先你需要从服务器解析数据。 然后使用该数据制作适配器并在 AutoCompleteTextView 中设置。
有关更多信息,我建议您查看 this blog
为了您的参考,还要检查 this link 以解析 ksoap Web 服务。
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();
}
}
用于从 ksoap web 服务获取自动完成文本视图数据的工作代码,使用搜索图标单击请求。
它将显示来自 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();
}
}
}