Monday, September 27, 2010

HttpConnection Time out

Here's the code

HttpPost httpPost = new HttpPost(url);
StringEntity se = new StringEntity(envelope,HTTP.UTF_8);
httppost.setEntity(se);

HttpParams httpParameters = new BasicHttpParams();
// Set the timeout in milliseconds until a connection is established.
int timeoutConnection = 3000;
HttpConnectionParams.setConnectionTimeout(httpParameters, timeoutConnection);
// Set the default socket timeout (SO_TIMEOUT)
// in milliseconds which is the timeout for waiting for data.
int timeoutSocket = 3000;
HttpConnectionParams.setSoTimeout(httpParameters, timeoutSocket);

DefaultHttpClient httpClient = new DefaultHttpClient(httpParameters);
BasicHttpResponse httpResponse = (BasicHttpResponse) httpclient.execute(httppost);

HttpEntity entity = httpResponse.getEntity();
return entity;

Thursday, September 23, 2010

Generic Xml Parsing in Android

Here's the code

public void genericXmlParsing(String raw) throws Exception{
DocumentBuilder builder = DocumentBuilderFactory
.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(new InputSource(new StringReader(raw)));
NodeList elementNames = doc.getElementsByTagName("ElementName");
NodeList elementValues = doc.getElementsByTagName("ElementValue");
for(int i=0;i Element elementName = (Element)elementNames.item(i);
ParsedXmlData parsedXmlData = new ParsedXmlData();
parsedXmlData.setElementName(elementName.getFirstChild().getNodeValue());
parsedXmlDataList.add(parsedXmlData);
}
for(int i=0;i Element elementValue = (Element)elementValues.item(i);
ParsedXmlData parsedXmlData = parsedXmlDataList.get(i);
parsedXmlData.setElementValue(elementValue.getFirstChild().getNodeValue());
}
}

Wednesday, September 22, 2010

XML Layout for Android

Ques:

Is there a way to display a View, such as a TextView sideways using the XML layout file? I know you can rotate the view using code in an activity, but is there a way just to do it with layout?


here's the answer

android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:layout_weight="5"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:text="sideways dummy text sideways dummy text sideways dummy text sideways dummy text sideways dummy text sideways dummy text sideways dummy text"
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
android:text="Text Here"
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">



Sunday, September 19, 2010

GIF Animation In Android

The gif animation is supported in Android when GIF animation is played as Movie. Movie is a class provided to you by Android SDK.

In your main or any Activity File

public class AA extends Activity {

@Override
public void onCreate(Bundle savedInstanceState){

super.onCreate(savedInstanceStated);


setContentView(new MYGIFView());
}

}


Here's the class


private class MYGIFView extends View{

Movie movie,movie1;
InputStream is=null,is1=null;
long moviestart;
public GIFView(Context context) {
super(context);


Provide your own gif animation file

is=context.getResources().openRawResource(R.drawable.earth);
movie=Movie.decodeStream(is);

}

@Override
protected void onDraw(Canvas canvas) {

canvas.drawColor(Color.WHITE);
super.onDraw(canvas);
long now=android.os.SystemClock.uptimeMillis();
System.out.println("now="+now);
if (moviestart == 0) { // first time
moviestart = now;

}
System.out.println("\tmoviestart="+moviestart);
int relTime = (int)((now - moviestart) % movie.duration()) ;
System.out.println("time="+relTime+"\treltime="+movie.duration());
movie.setTime(relTime);
movie.draw(canvas,this.getWidth()/2-20,this.getHeight()/2-40);
this.invalidate();
}
}