Pages

Tuesday, September 27, 2011

Google Maps in Android


Here you can find how to use Google Maps in your Android applications and how to programmatically perform the following:

  • Change the views of Google Maps
  • Obtain the latitude and longitude of locations in Google Maps
  • Perform geocoding and reverse geocoding
  • Add markers to Google Maps

Read response from HTTPpost method using HttpResponse (or any InputStream) as a String


This piece of code reads all inputstream response into string. Inputstream means of reading data from a source in a byte-wise manner.

 You get the entity response an InputStream object from HttpPost execute method. Essentially StringBuilder is a lot faster, and can be converted a normal string with .toString() method.

For reading the content of HttpResponse use convertStreamToString(response.getEntity().getContent())

HttpResponse responseGet = httpclient.execute(httppost);
HttpEntity entity = responseGet.getEntity();
if (entity != null) {
InputStream inputStreamResponse = entity.getContent();
result = convertStreamToString(inputStreamResponse);
}else {
 // code here for a response othet than 200.  A response 200 means the webpage was ok
 // Other codes include 404 - not found, 301 - redirect etc...
 // Display the response line.
System.out.println("Unable to load page - " + responseGet.getStatusLine());
}

Work with HTTP Post Request in android


The POST method is used to request that the origin server accept the entity enclosed in the request as a new subordinate of the resource identified by the Request-URI in the Request-Line.
learn more about Httppost 


public void postData() {
    // Create a new HttpClient and Post Header
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost("http://www.mysite.com/run.php");

    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("id", "12345"));
        nameValuePairs.add(new BasicNameValuePair("stringdata", "HTTPPOST example!"));
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // Execute HTTP Post Request
        HttpResponse response = httpclient.execute(httppost);
       
    } catch (ClientProtocolException e) {
        // TODO Auto-generated catch block
    } catch (IOException e) {
        // TODO Auto-generated catch block
    }
}

Wednesday, September 21, 2011

Create API Key for Google map for android


Generate the Certificate Fingerprint 
On Windows, open up a Command Prompt.
In the Command Prompt window, use the cd command to change to the .android subdirectory underneath your "home" directory, e.g. C:\Documents and Settings\xxxx\ where xxxx is your user name.

C:\> cd "\Documents and Settings\xxxx\.android"
Type in the following keytool command.

C:\> keytool -list -keystore debug.keystore

Note: if the system PATH is not set with the Java Development Kit's bin folder, then the full path to the keytool executable in the JDK/bin/ directory need to be used. For example,