Guest User

Untitled

a guest
Mar 19th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.12 KB | None | 0 0
  1. SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON
  2.  
  3. GOCREATETABLE A_Ciudades ( [IdCiudad] [int] NOT NULL, [NombreCiudad] [varchar](35)
  4.  
  5. NOT NULL, PRIMARY KEY CLUSTERED ( [IdCiudad] ASC )WITH (PAD_INDEX = OFF,
  6.  
  7. STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON,
  8.  
  9. ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO
  10.  
  11. using System;
  12. using System.Collections.Generic;
  13. using System.Linq;
  14. using System.Runtime.Serialization;
  15. using System.ServiceModel;
  16. using System.ServiceModel.Web;
  17. using System.Text;
  18. using System.Data;
  19. using System.Data.SqlClient;
  20.  
  21. namespace WcfService1
  22. {
  23. public class Service1 : IService1
  24. {
  25. private string conStr = "Data Source =.; Initial Catalog=.; uid=sa; pwd=.";
  26.  
  27. public List<A_Ciudades> GetA_Ciudades()
  28. {
  29. List<A_Ciudades> A_CiudadesList = new List<A_Ciudades>();
  30. SqlConnection connection = new SqlConnection(this.conStr);
  31. connection.Open();
  32. SqlCommand cmd = new SqlCommand("select NombreCiudad from A_Ciudades order by IdCiudad desc", connection);
  33. cmd.CommandType = CommandType.Text;
  34. SqlDataReader sdr = cmd.ExecuteReader();
  35. while (sdr.Read())
  36. {
  37. A_Ciudades rha = new A_Ciudades ();
  38. rha.NombreCiudad= sdr["NombreCiudad"].ToString();
  39. A_CiudadesList.Add(rha);
  40. }
  41. return A_Ciudades List.ToList();
  42. }
  43. }
  44. }
  45.  
  46. IService1.cs
  47.  
  48. using System;
  49. using System.Collections.Generic;
  50. using System.Linq;
  51. using System.Runtime.Serialization;
  52. using System.ServiceModel;
  53. using System.ServiceModel.Web;
  54. using System.Text;
  55. using System.Data;
  56. using System.Data.SqlClient;
  57.  
  58. namespace WcfService1
  59. {
  60. [ServiceContract]
  61. public interface IService1
  62. {
  63.  
  64. [OperationContract]
  65. [WebGet(UriTemplate = "GetA_Ciudades")]
  66. List<A_Ciudades> GetA_Ciudades ();
  67. }
  68. [DataContract]
  69. public class A_Ciudades
  70. {
  71. string nombreciudad; [DataMember] public string NombreCiudad { get { return nombreciudad; } set { nombreciudad = value; } }
  72. }
  73. }
  74.  
  75. <android.support.design.widget.AppBarLayout
  76. android:layout_width="match_parent"
  77. android:layout_height="wrap_content"
  78. android:theme="@style/AppTheme.AppBarOverlay">
  79. <android.support.v7.widget.Toolbar
  80. android:id="@+id/toolbar"
  81. android:layout_width="match_parent"
  82. android:layout_height="?attr/actionBarSize"
  83. android:background="?attr/colorPrimary"
  84. app:popupTheme="@style/AppTheme.PopupOverlay">
  85. <TextView
  86. android:id="@+id/date"
  87. android:layout_width="wrap_content"
  88. android:layout_height="wrap_content"
  89. android:text=""
  90. android:textColor="@color/colorTexto"
  91. android:textSize="20sp"
  92. />
  93. </android.support.v7.widget.Toolbar>
  94. </android.support.design.widget.AppBarLayout>
  95. <include layout="@layout/ cont_ciudades"/>
  96.  
  97. <GridView
  98. android:id="@+id/txtGrid"
  99. android:paddingLeft="6dp"
  100. android:paddingRight="6dp"
  101. android:paddingTop="6dp"
  102. android:paddingBottom="6dp"
  103. android:verticalSpacing="6dp"
  104. android:layout_width="wrap_content"
  105. android:layout_height="180dp"
  106. android:layout_gravity="center"
  107. android:animationCache="true"
  108. android:columnWidth="180dp"
  109. android:isScrollContainer="false"
  110. android:numColumns="2"
  111. android:stretchMode="spacingWidth"
  112. android:textFilterEnabled="false"
  113. tools:layout_editor_absoluteX="8dp"
  114. tools:layout_editor_absoluteY="8dp"
  115. android:layout_alignParentBottom="true"
  116. android:layout_alignParentLeft="true"
  117. android:layout_alignParentStart="true”>
  118. </GridView>
  119.  
  120. <TextView
  121. android:id="@+id/txtNombre"
  122. style="@android:style/Widget.Button.Inset"
  123. android:layout_width="178dp"
  124. android:layout_height="36dp"
  125. android:layout_alignParentLeft="true"
  126. android:layout_alignParentStart="true"
  127. android:layout_alignParentTop="true"
  128. android:gravity="center"
  129. android:text="Nombre Ciudad"
  130. android:textAppearance="@style/TextAppearance.AppCompat"
  131. android:textColor="@color/colorPrimaryDark"
  132. android:textSize="14dp"
  133. android:textStyle="bold" />
  134.  
  135. <TextView
  136. android:id="@+id/txtNombreCiudad"
  137. android:layout_width="89dp"
  138. android:layout_height="24dp"
  139. android:layout_alignParentLeft="true"
  140. android:layout_alignParentStart="true"
  141. android:layout_below="@+id/ txtNombre "
  142. android:gravity="bottom|center"
  143. android:text=" "
  144. android:textAppearance="@style/TextAppearance.AppCompat"
  145. android:textColor="@color/colorFondo"
  146. android:textSize="14dp"
  147. android:textStyle="bold" />
  148.  
  149. <ImageView
  150. android:id="@+id/txtImg"
  151. android:layout_width="wrap_content"
  152. android:layout_height="wrap_content"
  153. android:layout_below="@+id/ txtNombre "
  154. android:layout_toEndOf="@+id/ txtNombreCiudad "
  155.  
  156. import android.app.Activity;
  157. import android.content.Intent;
  158. import android.os.AsyncTask;
  159. import android.support.v7.app.ActionBar;
  160. import android.support.v7.app.AppCompatActivity;
  161. import android.os.Bundle;
  162. import android.support.v7.widget.Toolbar;
  163. import android.view.View;
  164. import android.widget.AdapterView;
  165. import android.widget.Button;
  166. import android.widget.GridView;
  167. import android.widget.ImageView;
  168. import android.widget.ListAdapter;
  169. import android.widget.SimpleAdapter;
  170. import android.widget.TextView;
  171. import android.widget.Toast;
  172. import org.json.JSONArray;
  173. import org.json.JSONObject;
  174. import java.io.BufferedInputStream;
  175. import java.io.BufferedReader;
  176. import java.io.InputStream;
  177. import java.io.InputStreamReader;
  178. import java.net.HttpURLConnection;
  179. import java.net.URL;
  180. import java.text.SimpleDateFormat;
  181. import java.util.ArrayList;
  182. import java.util.HashMap;
  183.  
  184. public class Ciudades extends AppCompatActivity {
  185.  
  186. ArrayList<HashMap<String, String>> CiudGrid;
  187. ListAdapter adapter;
  188.  
  189.  
  190. @Override
  191. protected void onCreate(Bundle savedInstanceState) {
  192. super.onCreate(savedInstanceState);
  193. setContentView(R.layout.ciudades);
  194.  
  195.  
  196. Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
  197. setSupportActionBar(toolbar);
  198. ActionBar actionBar = getSupportActionBar();
  199. actionBar.setDisplayHomeAsUpEnabled(true);
  200.  
  201. Thread t = new Thread() {
  202. @Override
  203. public void run() {
  204. try {
  205. while (!isInterrupted()) {
  206. Thread.sleep(500);
  207. runOnUiThread(new Runnable() {
  208. @Override
  209. public void run() {
  210. TextView tdate = (TextView) findViewById(R.id.date);
  211. long date = System.currentTimeMillis();
  212. SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yyyy");
  213. String dateString = sdf.format(date);
  214. tdate.setText(dateString);
  215. }
  216. });
  217. }
  218. } catch (InterruptedException e) {
  219. }
  220. }
  221. };
  222. t.start();
  223.  
  224.  
  225. CiudGrid = new ArrayList<HashMap<String, String>>();
  226. GridView Grid = (GridView) findViewById(R.id.txtGrid);
  227. new GetA_CiudadesList(Ciudades.this,Grid).execute("http://0.0.0.00:0000/service1.svc/GetA_Ciudades");
  228.  
  229. }
  230.  
  231. class GetA_CiudadesList extends AsyncTask<String, Void, String> {
  232. String status= null;
  233. Activity context;
  234. GridView gridView;
  235.  
  236.  
  237. public GetA_CiudadesList(Activity context, GridView gridView){
  238. this.context =context;
  239. this.gridView=gridView;
  240. }
  241.  
  242. protected void onPreExecute(){
  243.  
  244.  
  245. }
  246.  
  247. protected String doInBackground(String... connUrl){
  248. HttpURLConnection conn=null;
  249. BufferedReader reader;
  250. try{
  251. final URL url=new URL(connUrl[0]);
  252. conn=(HttpURLConnection) url.openConnection();
  253. conn.addRequestProperty("Content-Type", "application/json; charset=utf-8");
  254. conn.setRequestMethod("GET");
  255. int result = conn.getResponseCode();
  256. if(result==200){
  257. InputStream in=new BufferedInputStream(conn.getInputStream());
  258. reader = new BufferedReader(new InputStreamReader(in));
  259. StringBuilder sb=new StringBuilder();
  260. String line = null;
  261. while((line=reader.readLine())!=null){
  262. status=line;
  263. }
  264. }
  265. }catch(Exception ex){
  266. ex.printStackTrace();
  267. }
  268. return status;
  269. }
  270.  
  271. protected void onPostExecute(String result){
  272. super.onPostExecute(result);
  273. if(result!=null){
  274.  
  275. try{
  276. ArrayList<String> stringArrayList = new ArrayList<String>();
  277. JSONArray jsonArray = new JSONArray(result);
  278. for(int i=0; i<jsonArray.length(); i++){
  279. JSONObject object = jsonArray.getJSONObject(i);
  280. String NombreCiudad= object.getString("NombreCiudad");
  281. HashMap<String, String> itemList = new HashMap<String, String>();
  282. itemList.put("NombreCiudad",NombreCiudad);
  283. CiudGrid.add(itemList);
  284. }
  285. adapter = new SimpleAdapter(Ciudades.this, CiudGrid,R.layout.ciudades_list,
  286.  
  287. new String[]
  288. {
  289. "NombreCiudad"
  290. },
  291. new int[]
  292. {
  293. R.id.txtNombreCiudad,
  294.  
  295. });
  296.  
  297.  
  298. ((AdapterView<ListAdapter>) gridView).setAdapter(adapter);
  299.  
  300. }catch (Exception ex){
  301. ex.printStackTrace();
  302. }
  303. }else{
  304. Toast.makeText(A5_0_Result_Anim_Trad.this,"Could not get any data.",Toast.LENGTH_LONG).show();
  305. }
  306.  
  307. }
  308.  
  309. }
  310. }
Add Comment
Please, Sign In to add comment