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:
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
());
}
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---