本文介绍了从KSOAP web服务TextView的自动完成数据的提取,使用搜索图标的onClick要求与异步任务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何才能从KSOAP Web服务,展现在机器人自动完成TextView的搜索建议,通过点击搜索按钮获取数据。
How can we fetch Data from ksoap web service, show in Android Autocomplete textview search suggestion, using Onclick Search button.
推荐答案
工作code。对于自动完成的TextView数据KSOAP web服务获取,使用搜索图标,点击请求。
Working code For Autocomplete textview data Fetch from ksoap webservice, Using Search icon Click Request .
它会显示从KSOAP的WebService的建议。
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();
}
}
}
这篇关于从KSOAP web服务TextView的自动完成数据的提取,使用搜索图标的onClick要求与异步任务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!