This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

connectivity code java file

By: brutual123 on Oct 13th, 2011  |  syntax: None  |  size: 3.81 KB  |  views: 169  |  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.adigroup;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStream;
  5. import java.io.InputStreamReader;
  6. import java.util.ArrayList;
  7.  
  8. import org.apache.http.HttpEntity;
  9. import org.apache.http.HttpResponse;
  10. import org.apache.http.NameValuePair;
  11. import org.apache.http.client.HttpClient;
  12. import org.apache.http.client.entity.UrlEncodedFormEntity;
  13. import org.apache.http.client.methods.HttpPost;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15. import org.apache.http.message.BasicNameValuePair;
  16. import org.json.JSONArray;
  17. import org.json.JSONException;
  18. import org.json.JSONObject;
  19.  
  20. import android.app.Activity;
  21. import android.os.Bundle;
  22. import android.util.Log;
  23. import android.widget.Toast;
  24.  
  25. public class TestActivity extends Activity {
  26.     InputStream is;
  27.     @Override
  28.     public void onCreate(Bundle savedInstanceState) {
  29.         super.onCreate(savedInstanceState);
  30.         setContentView(R.layout.main);
  31.         String result = "";
  32.         //the year data to send
  33.         ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
  34.         nameValuePairs.add(new BasicNameValuePair("name","tushar"));
  35.         nameValuePairs.add(new BasicNameValuePair("password","123456"));
  36.         //nameValuePairs.add(new BasicNameValuePair("username",""));
  37.  
  38.        
  39.        
  40.        
  41.  
  42.         //http post
  43.         try{
  44.                 HttpClient httpclient = new DefaultHttpClient();
  45.                 HttpPost httppost = new HttpPost("http://localhost/connect1.php");
  46.                 httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  47.                 HttpResponse response = httpclient.execute(httppost);
  48.                 HttpEntity entity = response.getEntity();
  49.                 is = entity.getContent();
  50.                 Log.e("log_tag", "connection success ");
  51.                 Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
  52.         }catch(Exception e){
  53.                 Log.e("log_tag", "Error in http connection "+e.toString());
  54.                 Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
  55.  
  56.         }
  57.         //convert response to string
  58.         try{
  59.                 BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
  60.                 StringBuilder sb = new StringBuilder();
  61.                 String line = null;
  62.                 while ((line = reader.readLine()) != null) {
  63.                         sb.append(line + "\n");
  64.                         Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
  65.                 }
  66.                 is.close();
  67.  
  68.                 result=sb.toString();
  69.         }catch(Exception e)
  70.         {
  71.                Log.e("log_tag", "Error converting result "+e.toString());
  72.                System.out.println("app in progress********************" + nameValuePairs.size());
  73.             Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
  74.  
  75.         }
  76.  
  77.         //parse json data
  78.         try{
  79.                 JSONArray jArray = new JSONArray(result);
  80.                 for(int i=0;i<jArray.length();i++){
  81.                        JSONObject json_data = jArray.getJSONObject(i);
  82.                         Log.e("log_tag","name: "+json_data.getInt("name")+
  83.                                 ", name: "+json_data.getString("name")+
  84.                                 ", sex: "+json_data.getInt("sex")+
  85.                                 ", birthyear: "+json_data.getInt("birthyear")
  86.                         );
  87.                         Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
  88.                }
  89.  
  90.         }catch(JSONException e){
  91.                 Log.e("log_tag", "Error parsing data "+e.toString());
  92.                 Toast.makeText(getApplicationContext(), "fail", Toast.LENGTH_SHORT).show();
  93.         }
  94.     }
  95. }
  96.  
  97.  
clone this paste RAW Paste Data