Monday, August 22, 2011

Android and JSON

Here is basic JSON string which you will extract,Consider a string


{Question: {Q_id:111,Q_Name:How much is ur salry, TypeOfQuest: {Type:CheckBox,Values:[<50,>50,=50]}}}

The present string needs to be converted in to JSON form for doing this you can use String Builder Object else tou can convert manually
as below

Str="{\"Question\": {\"Q_id\":\"111\",\"Q_Name\":\"How much is ur salry\", \"TypeOfQuest\": {\"Type\":\"CheckBox\", " +
"\"Values\":[\"<50\",\">50\",\"=50\"]}}}";

Now is the Extracting of the JSON String

jObject = new JSONObject(Str);
  JSONObject Question = jObject.getJSONObject("Question");
  Q_id = Question.getString("Q_id");
  System.out.println(Q_id);

      Q_Name = Question.getString("Q_Name");
  System.out.println(Q_Name);

  JSONObject TypeOfQuest = Question.getJSONObject("TypeOfQuest");
  Type = TypeOfQuest.getString("Type");
  System.out.println(Type);
  ValuesArray = TypeOfQuest.getJSONArray("Values");
  //System.out.println(ValuesArray.length());

  for (int i = 0; i < ValuesArray.length(); i++) {
   //System.out.println(ValuesArray.getJSONArray(0).toString());
   System.out.println(ValuesArray.getString(i).toString());


That is it..........................

Connecting anDroiD wid the Remote Server

This is will be pre-requisite in all Android app as you will need to bring or send data or for advertisement. Using Http Request Android Application can connect to remote server.


HttpClient httpclient = new DefaultHttpClient();

HttpPost httppost = new HttpPost("www.yourUrl.com");


Here HttpClient Object is created and you are using post method. Using Http post method you can send and get data from server,whereas using Http Get you can only get the data.







Friday, August 19, 2011

How to get MD 5 fingerprint key and Google Map Api key for anDroiD



  • In the Command prompt be in bib folder of the JDK , which must have installed before using Eclipse               C:\Program Files\Java\jdk1.6.0_26\bin

  • Then use this command line to get the MD5 key on the windows(both XP and 7)                                          keytool -list -alias androiddebugkey -keystore "C:\Users\user_name\.android\debug.keystore" -storepass android
           
  • When you click the enter button you will get a MD5 key. Example:F3:11:23:D2:35:6C:C3:8E:7B:ED:A3:93:DA:4C:95:8D



  • The complete code                                                                                                                 C:\Program Files\Java\jdk1.6.0_26\bin>keytool -list -alias androiddebugkey -keystore "C:\Users\user_name\.android\debug.keystore" -storepass android                            

  • Then go to http://code.google.com/android/maps-api-signup.html and insert the MD5 ,It will give you the Google Map Api Key!!!!!!!!!!!!!!!!!

Thursday, August 11, 2011

How to get data from web pages


DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.someplace.com");
ResponseHandler<String> resHandler = new BasicResponseHandler();
String page = httpClient.execute(httpGet, resHandler);
This can be used to grab the whole data from the web pages