[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

10/28/2009 at 11:22PM PDT, ID: 24853738 | Points: 500
[x]
Attachment Details

How to connect through HttpConnection using WiFi for Android?

Asked by retinax in Handheld and PDA Programming

Hi Friends....

I have written my HttpConnection code for Android.I have also set Network preferences. Should I change the code so that it would be able to work for Wifi?or I dont need any change.Does the same code work for Wifi correctly?Here is my code:
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
public static String http_get(String urlParameters)
          {
                    String msg = "",uid = "";
                    int ch;
                  
 
                    url = url + urlParameters ;
   
                    int BUFFER_SIZE = 2000;
                    InputStream in = null;
                    try {
                        in = OpenHttpConnection(url);
                    } catch (IOException e1) {
                        // TODO Auto-generated catch block
                        e1.printStackTrace();
                      
                    }
 
                    try
                    {
                    	 InputStreamReader isr = new InputStreamReader(in);
                         int charRead;
                           String str = "";
                           char[] inputBuffer = new char[BUFFER_SIZE];          
                         try {
                             while ((charRead = isr.read(inputBuffer))>0)
                             {                    
                                 //---convert the chars to a String---
                                 String readString = 
                                     String.copyValueOf(inputBuffer, 0, charRead);                    
                                 str += readString;
                                 inputBuffer = new char[BUFFER_SIZE];
                             }
                             in.close();
                         } catch (IOException e) {
                             // TODO Auto-generated catch block
                             e.printStackTrace();
                            
                         }   
                         Log.i("Server",str);
                              
                              if(str.indexOf("1_")!=-1)
                              {
                            	 
                                    uid = str.substring(2);
                                   
                                    
                              }
                              else 
                              {
                                  
                            	 uid="0";
                                  
                              }
                              
                             
                    }
                    catch (Exception e)
                    {
                              msg = e.toString();
                    }
                    return uid;
          }
 
  public static String postData(String user,String file, String Contents){   
              
              // Create a new HttpClient and Post Header   
              HttpClient httpclient = new DefaultHttpClient();   
              HttpPost httppost = new HttpPost(url+file);   
              String result="";
 
          try {   
               // Add your data   
               List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
               Log.i("POSTDATA",user);
               nameValuePairs.add(new BasicNameValuePair("sID", user));   
               nameValuePairs.add(new BasicNameValuePair("content", Contents));  
               httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));   
      
               // Execute HTTP Post Request   
               HttpResponse response = httpclient.execute(httppost); 
                
               InputStream is = response.getEntity().getContent(); 
               BufferedInputStream bis = new BufferedInputStream(is); 
               ByteArrayBuffer baf = new ByteArrayBuffer(20); 
 
                int current = 0;   
                while((current = bis.read()) != -1){   
                    baf.append((byte)current);   
                }   
                  Log.d("GETANDPOST",new String(baf.toByteArray()));
                  result=new String(baf.toByteArray());
               /* Convert the Bytes read to a String. */   
          //     text = new String(baf.toByteArray()); 
             //  tv.setText(text); 
      
          } catch (ClientProtocolException e) {   
               // TODO Auto-generated catch block   
          } catch (IOException e) {   
               // TODO Auto-generated catch block   
          } 
          return result;
     } 
 
public String SetPreference() { 
        ConnectivityManager 
connMgr=(ConnectivityManager)getSystemService(this.CONNECTIVITY_SERVICE); 
        NetworkInfo info=connMgr.getActiveNetworkInfo(); 
        connMgr.setNetworkPreference(ConnectivityManager.TYPE_WIFI);
                   return (connMgr.getActiveNetworkInfo().getTypeName());
 
 
      
 
 
}
 
Loading Advertisement...
20090824-EE-VQP-74 - Hierarchy / EE_QW_3_20080625