Don't like ads? PRO users don't see any ads ;-)
Guest

main.java

By: a guest on Apr 24th, 2012  |  syntax: None  |  size: 6.86 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. package com.hamdyghanem.httprequest;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.util.ArrayList;
  6. import java.util.HashMap;
  7. import java.util.List;
  8.  
  9. import org.apache.http.HttpEntity;
  10. import org.apache.http.HttpResponse;
  11. import org.apache.http.NameValuePair;
  12. import org.apache.http.client.ClientProtocolException;
  13. import org.apache.http.client.HttpClient;
  14. import org.apache.http.client.ResponseHandler;
  15. import org.apache.http.client.entity.UrlEncodedFormEntity;
  16. import org.apache.http.client.methods.HttpGet;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.entity.ByteArrayEntity;
  19. import org.apache.http.impl.client.BasicResponseHandler;
  20. import org.apache.http.impl.client.DefaultHttpClient;
  21. import org.apache.http.message.BasicNameValuePair;
  22. import org.apache.http.params.BasicHttpParams;
  23. import org.apache.http.params.HttpConnectionParams;
  24. import org.apache.http.params.HttpParams;
  25. import org.json.JSONArray;
  26. import org.json.JSONObject;
  27. import org.json.JSONTokener;
  28.  
  29. import com.google.gson.Gson;
  30. import com.google.gson.JsonArray;
  31. import com.google.gson.JsonParser;
  32.  
  33. import android.app.Activity;
  34. import android.app.ProgressDialog;
  35. import android.content.ContentProviderOperation;
  36. import android.content.ContentResolver;
  37. import android.content.Context;
  38. import android.database.Cursor;
  39. import android.graphics.Typeface;
  40. import android.os.AsyncTask;
  41. import android.os.Bundle;
  42. import android.os.Handler;
  43. import android.os.Message;
  44. import android.provider.ContactsContract;
  45. import android.provider.ContactsContract.CommonDataKinds.Phone;
  46. import android.text.Html;
  47. import android.text.method.LinkMovementMethod;
  48. import android.util.Log;
  49. import android.view.Display;
  50. import android.view.Gravity;
  51. import android.view.View;
  52. import android.view.WindowManager.LayoutParams;
  53. import android.widget.TableLayout;
  54. import android.widget.TableRow;
  55. import android.widget.TextView;
  56. import android.widget.Toast;
  57.  
  58. public class Main extends Activity {
  59.         /** Called when the activity is first created. */
  60.         public int iLanguage = 0;
  61.         TextView lbl;
  62.         Typeface arabicFont = null;
  63.         int TIMEOUT_MILLISEC = 10000; // = 10 seconds
  64.  
  65.         @Override
  66.         public void onCreate(Bundle savedInstanceState) {
  67.                 try {
  68.  
  69.                         super.onCreate(savedInstanceState);
  70.                         setContentView(R.layout.main);
  71.                         getWindow().setLayout(LayoutParams.FILL_PARENT,
  72.                                         LayoutParams.FILL_PARENT);
  73.                         // ////
  74.                         arabicFont = Typeface.createFromAsset(getAssets(),
  75.                                         "fonts/DroidSansArabic.ttf");
  76.  
  77.                         lbl = (TextView) findViewById(R.id.lbl);
  78.                         //
  79.  
  80.                 } catch (Throwable t) {
  81.                         Toast.makeText(this, "Request failed: " + t.toString(),
  82.                                         Toast.LENGTH_LONG).show();
  83.                 }
  84.         }
  85.  
  86.         public void clickbuttonRecieve(View v) {
  87.                 try {
  88.                         JSONObject json = new JSONObject();
  89.                         json.put("UserName", "test2");
  90.                         json.put("FullName", "1234567");
  91.                         HttpParams httpParams = new BasicHttpParams();
  92.                         HttpConnectionParams.setConnectionTimeout(httpParams,
  93.                                         TIMEOUT_MILLISEC);
  94.                         HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
  95.                         HttpClient client = new DefaultHttpClient(httpParams);
  96.                         //
  97.                         //String url = "http://10.0.2.2:8080/sample1/webservice2.php?json={\"UserName\":1,\"FullName\":2}";
  98.                         String url = "http://10.0.2.2:8080/sample1/webservice2.php";
  99.  
  100.                         HttpPost request = new HttpPost(url);
  101.                         request.setEntity(new ByteArrayEntity(json.toString().getBytes(
  102.                                         "UTF8")));
  103.                         request.setHeader("json", json.toString());
  104.                         HttpResponse response = client.execute(request);
  105.                         HttpEntity entity = response.getEntity();
  106.                         // If the response does not enclose an entity, there is no need
  107.                         if (entity != null) {
  108.                                 InputStream instream = entity.getContent();
  109.  
  110.                                 String result = RestClient.convertStreamToString(instream);
  111.                                 Log.i("Read from server", result);
  112.                                 Toast.makeText(this,  result,
  113.                                                 Toast.LENGTH_LONG).show();
  114.                         }
  115.                 } catch (Throwable t) {
  116.                         Toast.makeText(this, "Request failed: " + t.toString(),
  117.                                         Toast.LENGTH_LONG).show();
  118.                 }
  119.         }
  120.  
  121.         public void clickbutton(View v) {
  122.                 try {
  123.                         // http://androidarabia.net/quran4android/phpserver/connecttoserver.php
  124.  
  125.                         // Log.i(getClass().getSimpleName(), "send  task - start");
  126.                         HttpParams httpParams = new BasicHttpParams();
  127.                         HttpConnectionParams.setConnectionTimeout(httpParams,
  128.                                         TIMEOUT_MILLISEC);
  129.                         HttpConnectionParams.setSoTimeout(httpParams, TIMEOUT_MILLISEC);
  130.                         //
  131.                         HttpParams p = new BasicHttpParams();
  132.                         // p.setParameter("name", pvo.getName());
  133.                         p.setParameter("user", "1");
  134.  
  135.                         // Instantiate an HttpClient
  136.                         HttpClient httpclient = new DefaultHttpClient(p);
  137.                         String url = "http://10.0.2.2:8080/sample1/webservice1.php?user=1&format=json";
  138.                         HttpPost httppost = new HttpPost(url);
  139.  
  140.                         // Instantiate a GET HTTP method
  141.                         try {
  142.                                 Log.i(getClass().getSimpleName(), "send  task - start");
  143.                                 //
  144.                                 List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
  145.                                                 2);
  146.                                 nameValuePairs.add(new BasicNameValuePair("user", "1"));
  147.                                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  148.                                 ResponseHandler<String> responseHandler = new BasicResponseHandler();
  149.                                 String responseBody = httpclient.execute(httppost,
  150.                                                 responseHandler);
  151.                                 // Parse
  152.                                 JSONObject json = new JSONObject(responseBody);
  153.                                 JSONArray jArray = json.getJSONArray("posts");
  154.                                 ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
  155.  
  156.                                 for (int i = 0; i < jArray.length(); i++) {
  157.                                         HashMap<String, String> map = new HashMap<String, String>();
  158.                                         JSONObject e = jArray.getJSONObject(i);
  159.                                         String s = e.getString("post");
  160.                                         JSONObject jObject = new JSONObject(s);
  161.  
  162.                                         map.put("idusers", jObject.getString("idusers"));
  163.                                         map.put("UserName", jObject.getString("UserName"));
  164.                                         map.put("FullName", jObject.getString("FullName"));
  165.  
  166.                                         mylist.add(map);
  167.                                 }
  168.                                 Toast.makeText(this, responseBody, Toast.LENGTH_LONG).show();
  169.  
  170.                         } catch (ClientProtocolException e) {
  171.                                 // TODO Auto-generated catch block
  172.                                 e.printStackTrace();
  173.                         } catch (IOException e) {
  174.                                 // TODO Auto-generated catch block
  175.                                 e.printStackTrace();
  176.                         }
  177.                         // Log.i(getClass().getSimpleName(), "send  task - end");
  178.  
  179.                 } catch (Throwable t) {
  180.                         Toast.makeText(this, "Request failed: " + t.toString(),
  181.                                         Toast.LENGTH_LONG).show();
  182.                 }
  183.  
  184.         }
  185.  
  186.         public class Data {
  187.                 // private List<User> users;
  188.                 public List<User> users;
  189.  
  190.                 // +getters/setters
  191.         }
  192.  
  193.         static class User {
  194.                 String idusers;
  195.                 String UserName;
  196.                 String FullName;
  197.  
  198.                 public String getUserName() {
  199.                         return UserName;
  200.                 }
  201.  
  202.                 public String getidusers() {
  203.                         return idusers;
  204.                 }
  205.  
  206.                 public String getFullName() {
  207.                         return FullName;
  208.                 }
  209.  
  210.                 public void setUserName(String value) {
  211.                         UserName = value;
  212.                 }
  213.  
  214.                 public void setidusers(String value) {
  215.                         idusers = value;
  216.                 }
  217.  
  218.                 public void setFullName(String value) {
  219.                         FullName = value;
  220.                 }
  221.         }
  222. }