Guest User

android

a guest
Aug 30th, 2011
1,144
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.87 KB | None | 0 0
  1. package com.sencide;
  2.  
  3.  
  4.  
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.net.SocketException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. import org.apache.http.NameValuePair;
  11. import android.app.Activity;
  12. import android.app.AlertDialog;
  13. import android.content.DialogInterface;
  14. import android.os.Bundle;
  15. import android.util.Log;
  16. import android.widget.TextView;
  17. import org.apache.http.client.entity.UrlEncodedFormEntity;
  18. import org.apache.http.HttpEntity;
  19. import org.apache.http.HttpResponse;
  20. import org.apache.http.client.ClientProtocolException;
  21. import org.apache.http.client.HttpClient;
  22. import org.apache.http.client.methods.HttpPost;
  23. import org.apache.http.impl.client.DefaultHttpClient;
  24. import org.apache.http.message.BasicNameValuePair;
  25. import org.apache.http.protocol.HTTP;
  26. import org.json.JSONArray;
  27. import org.json.JSONException;
  28. import org.json.JSONObject;
  29. import org.ksoap2.SoapEnvelope;
  30. import org.ksoap2.serialization.SoapObject;
  31. import org.ksoap2.serialization.SoapPrimitive;
  32. import org.ksoap2.serialization.SoapSerializationEnvelope;
  33. import org.ksoap2.transport.HttpTransportSE;
  34.  
  35.  
  36. public class TestApp extends Activity {
  37.  
  38. private static final String SOAP_ACTION = "http://tempuri.org/getData";
  39.  
  40. private static final String METHOD_NAME = "getData";
  41.  
  42. private static final String NAMESPACE = "http://tempuri.org/";
  43. private static final String URL = "http://10.0.2.2/login2/Service1.asmx";
  44. TextView tv;
  45.  
  46. boolean[] bln1=null;
  47.  
  48. @Override
  49. public void onCreate(Bundle savedInstanceState) {
  50. super.onCreate(savedInstanceState);
  51. setContentView(R.layout.main);
  52. tv=(TextView)findViewById(R.id.text1);
  53.  
  54. String[] arr2= call();
  55.  
  56.  
  57. boolean[] bln=new boolean[arr2.length];
  58. for(int i=0;i<arr2.length;i++)
  59. {
  60.  
  61. bln[i]=false;
  62.  
  63. }
  64.  
  65. bln1 = new boolean[arr2.length];
  66.  
  67.  
  68.  
  69. new AlertDialog.Builder(TestApp.this)
  70. .setIcon(R.drawable.alert_dialog_icon)
  71. .setTitle("Title")
  72. .setMultiChoiceItems(arr2,
  73. bln,
  74. new DialogInterface.OnMultiChoiceClickListener() {
  75. public void onClick(DialogInterface dialog, int whichButton,
  76. boolean isChecked) {
  77.  
  78.  
  79. if(isChecked){
  80. bln1[whichButton] = true;
  81. }
  82. else{
  83. bln1[whichButton] = false;
  84. }
  85. }
  86. })
  87. .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  88. public void onClick(DialogInterface dialog, int whichButton) {
  89.  
  90. JSONObject jsonObject = new JSONObject();
  91. String[] arr2=call();
  92. for(int i=0;i<arr2.length;i++)
  93. try {
  94. jsonObject.put("key"+i,arr2[i]);
  95. } catch (JSONException e) {
  96. // TODO Auto-generated catch block
  97. e.printStackTrace();
  98. }
  99.  
  100. // jsonObject.put("key2", value2);
  101.  
  102. JSONArray jArrayParam = new JSONArray();
  103. jArrayParam.put(jsonObject);
  104.  
  105. List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>();
  106. nameValuePair.add(new BasicNameValuePair("bulkdata",
  107. jArrayParam.toString()));
  108.  
  109. Log.e("bulkdata", jArrayParam.toString());
  110.  
  111. HttpClient httpclient = new DefaultHttpClient();
  112. HttpPost httppost = new HttpPost("http://10.0.2.2/login2/Service1.asmx");
  113.  
  114. httppost.addHeader("Content-Type", "application/x-www-form-urlencoded");
  115. try {
  116. httppost.setEntity(new UrlEncodedFormEntity(nameValuePair, HTTP.UTF_8));
  117. } catch (UnsupportedEncodingException e1) {
  118. // TODO Auto-generated catch block
  119. e1.printStackTrace();
  120. }
  121. // Execute HTTP Post Request
  122.  
  123. try {
  124. HttpResponse response=null;
  125. response = httpclient.execute(httppost);
  126. HttpEntity entity = response.getEntity();
  127. Log.e("entity:",entity.toString());
  128. } catch (ClientProtocolException e) {
  129. // TODO Auto-generated catch block
  130. e.printStackTrace();
  131. } catch (IOException e) {
  132. // TODO Auto-generated catch block
  133. e.printStackTrace();
  134. }
  135. // get response entity
  136.  
  137. }
  138. })
  139. .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  140. public void onClick(DialogInterface dialog, int whichButton) {
  141.  
  142.  
  143. }
  144. })
  145. .show();
  146.  
  147.  
  148. }
  149.  
  150.  
  151. public String[] call()
  152. {
  153. SoapPrimitive responsesData = null;
  154.  
  155. SoapObject request = new SoapObject(NAMESPACE, METHOD_NAME);
  156.  
  157. SoapSerializationEnvelope envelope = new SoapSerializationEnvelope(
  158. SoapEnvelope.VER11);
  159. envelope.dotNet = true;
  160. envelope.setOutputSoapObject(request);
  161.  
  162. HttpTransportSE androidHttpTransport = new HttpTransportSE(URL);
  163.  
  164. androidHttpTransport.debug = true;
  165.  
  166. try {
  167.  
  168. androidHttpTransport.call(SOAP_ACTION, envelope);
  169.  
  170. responsesData = (SoapPrimitive) envelope.getResponse();
  171. System.out.println(" --- response ---- " + responsesData);
  172.  
  173. } catch (SocketException ex) {
  174. ex.printStackTrace();
  175. } catch (Exception e) {
  176. e.printStackTrace();
  177. }
  178. System.out.println( " ----" + responsesData );
  179.  
  180. String serviceResponse= responsesData .toString();
  181.  
  182.  
  183. String[] temp;
  184. String delimiter = "#";
  185. temp= serviceResponse.split(delimiter);
  186. System.out.println( " ---- length ---- " + temp.length);
  187.  
  188. return temp;
  189.  
  190.  
  191. }
  192.  
  193.  
  194.  
  195. }
Advertisement
Add Comment
Please, Sign In to add comment