Archive for July, 2012

WebResponce in Android

Posted: July 31, 2012 in Android
Tags: , ,

Hello Droids..!

Here i am demonstrating a class file which will be very helpful in getting web-response. I read so many comments on Web-Response that giving error in fetching.

So here is the .java file into which you just have to pass the URL! and in response you will get String value! That’s it!  Easy and handy!

Here is the Code,


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.StatusLine;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class WebResponse {

	public String getWebResponse(String URL)
	{
	  HttpClient httpclient = new DefaultHttpClient();
      HttpResponse response;
     String responseString = "";
      try {

//           String temp = URL;
//              temp = temp.replaceAll(" ","%20");

          response = httpclient.execute(new HttpGet(URL));
          StatusLine statusLine = response.getStatusLine();
          if(statusLine.getStatusCode() == HttpStatus.SC_OK){
              ByteArrayOutputStream out = new ByteArrayOutputStream();
              response.getEntity().writeTo(out);
              out.close();
              responseString = out.toString();

          } else{
              //Closes the connection.
              response.getEntity().getContent().close();
              throw new IOException(statusLine.getReasonPhrase());
          }
      } catch (ClientProtocolException e) {
          //TODO Handle problems..

      } catch (IOException e) {
          //TODO Handle problems..
      e.printStackTrace();
      }
      return responseString;
	}

}

How to use:

//first create an object of this class file
WebResponse wr = new WebResponse();
String resp = wr.getWebResponse(your url); //add your url

Download here:: WebResponse.java

Comments will be appreciated. 🙂

Advertisements

This is a sample app for integrating Facebook in android.

This will show you how to manage session(is session expired or not), how to handle it.

You are able to post on your own wall, with Title, Message, Link of app, Image and Properties of particular app.

You just have to provide your Facebook app id.

Here is the source code, Android Facebook Demo

Android Twitter Demo

Posted: July 18, 2012 in Android

Hello all,
This is a sample demo how to integrate twitter in android.
How to follow the OAUTH flow, i tried my best to make it simple and understandable.
Here is link for download sourccode,
Android Twitter Demo

If you found any mistake then leave a reply me on my post.