Android

Android SDK Manager does not open in windows 8

I have installed windows 8 in my laptop and tried to open ‘Android SDK Manager’. It was not open. By googling I found the solution as below.

Go to android-sdk folder what you had set path under windows->preference menu for android.
Edit android.bat file.
Find java_exe=
And in front of add Java.exe path like “C:\Program Files (x86)\Java\jdk1.6.0\bin\java.exe”

HttpResponse to String android

HttpClient httpclient;
	HttpPost httppost;
	HttpResponse response;
httppost = new HttpPost("website-url-goes-here");
try {
				response = httpclient.execute(httppost);
				HttpEntity entity1 = response.getEntity();
				InputStream is = entity1.getContent();
				
				String w1url = convertStreamToString(is);
				
				
			} catch (ClientProtocolException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}

Add function into a class

private static String convertStreamToString(InputStream is) {

	    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
	    StringBuilder sb = new StringBuilder();

	    String line = null;
	    try {
	        while ((line = reader.readLine()) != null) {
	            sb.append((line + "\n"));
	        }
	    } catch (IOException e) {
	        e.printStackTrace();
	    } finally {
	        try {
	            is.close();
	        } catch (IOException e) {
	            e.printStackTrace();
	        }
	    }
	    return sb.toString();
	}

Google cloud Messaging (GCM) for Android App and Asp.Net server app

At the Google IO 2012, the beta version of Android push notifications system called C2DM was replaced by Google Cloud Messaging (GCM). GCM has many new features over the existing system. Refer to this document for details of the differences and details about migrating your existing systems from C2DM to GCM.

To get started, download the Extras > Google Cloud Messaging for Android Library from the Android SDK.This library provides the jars to simplify the development on both the server side and client side.

From the Google API console page, create a new project and generate the API key. Refer this page for complete details.

Google provides the sample apps in the SDK by default, you may download a very basic version of them containing only the code discussed in this tutorial from here.
The same application is also available on play store here.

For .Net App download click here

Send free SMS API

http://ubaid.tk/api-usage/

Required Params : uid, pwd, phone, msg.

uid : your userid for the required sms provider
pwd : your password for the required sms provider
provider : way2sms(default), fullonsms, smsindia, whozzat, smsinside, site2sms. if you do not specify any provider, way2sms will be used by default.
phone : phone number whom you want to send sms. seperate multiple phone numbers with a comma (,)
msg : your sms message, unlimited chars. will be sent as multiple msgs if crosses the message length for any provider

Optional Parameters
codes : 1. Send this if you require a user friendly msg from the server. for example, if codes=1 is not provided the server will return the result as an integer.
1 – SMS sent
-1 – Server Error
-2 – Invalid Username
-3 – Invalid message text
-4 – Login Failed
-5 – IP Blocked

Alert Box in Android

protected void alertbox(String title, String mymessage)
{
new AlertDialog.Builder(this)
.setMessage(mymessage)
.setTitle(title)
.setCancelable(true)
.setNeutralButton(android.R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton){}
})
.show();
}

For more details see below link…
http://huuah.com/dialog-boxes-in-android/

How to parse json string object in Android

First of all you have understand what is jsonObject and jsonObjectArray.
ex. of jsonObject
{
“Validate”: “true”
}
if string start with “{” then it’s a only a object.
[
{
“Validate”: “true”
}
]
if string start with “[” then its array of object.

Here I describe both how to parse object or array of string as below.
Create a Class it has a one property.

public class Users {
public String Validate;
public void setUser(String Validate)
{
this.Validate = Validate;
}
public String getValidate()
{
return Validate;
}
}

—– bleow function parse json… create a new class which is parse json string
public class jsonParser {
JSONObject jsonObject;
JSONArray jsonArray;
Users objUser;

public jsonParser()
{
objUser = new Users();
}
public Users parseUser(String str)
{
try {
jsonObject = (JSONObject) new JSONObject(str);

objUser.companyId = jsonObject.getString(UserConstant.Company_Id);
objUser.FullName= jsonObject.getString(UserConstant.Full_Name);
objUser.LastLoginDate= jsonObject.getString(UserConstant.Last_Login_Date);
objUser.Password= jsonObject.getString(UserConstant.PASSWORD);
objUser.UserId = jsonObject.getString(UserConstant.User_Id);
objUser.Validate= jsonObject.getString(UserConstant.VALIDATE);
objUser.WrongPassCount= jsonObject.getInt(UserConstant.WRONGPASSCOUNT);
objUser.isActiveDirectory= jsonObject.getBoolean(UserConstant.IsActiveDirectory);

Log.d(“Json Result = ” , objUser.Validate);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
objUser=null;
}

return objUser;
}

public ArrayList parseUsers(String str)
{
ArrayList appUserList= new ArrayList();
Users user;
try {
jsonArray = (JSONArray) new JSONArray(str);
int arrayLength = jsonArray.length();

for(int i=0;i<arrayLength;i++)
{
user = new Users();
JSONObject arrayObject1 = jsonArray.getJSONObject(i);
user.setUser(arrayObject1.getString(UserConstant.Company_Id),
arrayObject1.getString(UserConstant.Full_Name),
arrayObject1.getString(UserConstant.Last_Login_Date),
arrayObject1.getString(UserConstant.PASSWORD),
arrayObject1.getString(UserConstant.User_Id),
arrayObject1.getString(UserConstant.VALIDATE),
arrayObject1.getInt(UserConstant.WRONGPASSCOUNT),
arrayObject1.getBoolean(UserConstant.IsActiveDirectory)
);
appUserList.add(user);
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

return appUserList;
}
}

Consume webservice in Android app

First of all give internet permission to app for that below steps.
1. Open “AndroidManifest.xml” file.
2. Copy below code under

Now make one function which is consume web service.
public String doRequest(String requestURL)
{
InputStream is = null;
String jsonString = “”;
HttpClient httpClient = null;
HttpGet request = null;

try {

HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters,20*1000); // Connection timeout
HttpConnectionParams.setSoTimeout(httpParameters, 20*1000); // Socket timeout

// Create a new HttpClient and Post Header
httpClient = new DefaultHttpClient(httpParameters);

request = new HttpGet(requestURL);

HttpResponse httpResponse = httpClient.execute(request);

is = httpResponse.getEntity().getContent();

byte buff[] = new byte[1024];
int c = 0;
while ((c = is.read(buff)) != -1) {
jsonString += new String(buff, 0, c);
}
// Log.d(tag, “JSON-String =” + jsonString);
} catch (Exception e) {

e.printStackTrace();
return null;

} finally {
// Close opened streams.
// FIX
try {
if (is != null)
is.close();

if (httpClient != null)
httpClient = null;

if (request != null)
request = null;

} catch (IOException e) {
e.printStackTrace();
}
}
// Log.d(“HttpRequest”, “### Returning ” + jsonString);
return jsonString;
}

— CAll this function when you want to get result of web service —
String requestStr = “http://salesbook.iglobeapps.com/json/loginservice.svc/validateuser?email=demo1&password=demo111”;
String str = doRequest(requestStr);