Monday, 24 September 2012

Connect to remote mysql db with PHP webservice, and parse the JSON response object returned using JAVA

Hi, here is a sample code on how to connect to a remote mysql db using a php and parse the JSON response object returned


                  HttpClient httpclient = new DefaultHttpClient();

                 Inputstream is;
                 HttpPost httppost = new HttpPost("http://www.hostname.com/files/getbrowsedata.php");
               HttpResponse response = httpclient.execute(httppost);
               HttpEntity entity = response.getEntity();
                 is = entity.getContent();
        }catch(Exception e){
               Log.e("log_tag", "Error in http connection "+e.toString());
                      }
       

        //convert response to string
        try{
               BufferedReader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"),8);
               StringBuilder sb = new StringBuilder();
               String line = null;
               while ((line = reader.readLine()) != null) {
                       sb.append(line + "\n");
               }
               System.out.println("sb::" +sb);
                String result=sb.toString();
               is.close();
       

        }catch(Exception e){
               Log.e("log_tag", "Error in http connection "+e.toString());
         }

//parse the JSON response object
 try{
       
        JSONObject json = new JSONObject(result);
        JSONArray json_array = json.getJSONArray("display");
       
            for (int i = 0; i < json_array.length(); i++) {
                JSONObject result_json_object = json_array.getJSONObject(i);
                String column1= result_json_object.getString("col_name_1");
              String column2= result_json_object.getString("col_name_2");
   
                System.out.println(" column1 ::"+ column2 );
             
                }
            }
        }catch(JSONException e){
             Log.e("log_tag", "Error parsing data "+e.toString());
                
        }


Thursday, 20 September 2012

How to access a .properties file on remote server using Java

One of the simplest ways to access a .properties file on remote server is by sending a HTTP request. Similarly any other type of file can also be accessed by using the appropriate API
It can be done in the following way...


Properties prop = new Properties();

    try {
               //load a properties file
    HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new  HttpPost("http://hostname/xxx.properties");
    HttpResponse response =   httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    InputStream is = entity.getContent();
    prop.load(is);

    //get the property value and print it out
    System.out.println(prop.getProperty("url"));
   
    } catch (IOException ex) {
    ex.printStackTrace();
        }

    }

Monday, 3 September 2012

Fix for Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException

Problem:
Fix for Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONException in eclipse IDE

Cause:
json jar not found in the class path

Fix:
Download json jar
www.java2s.com/Code/Jar/j/Downloadjavajsonjar.htm

Select project->Build path->Add external jar->add the jar->clean and build the project

Sunday, 2 September 2012

Fix for Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/ref/FinalReference

Problem:
 Error occurred during initialization of VM java/lang/NoClassDefFoundError: java/lang/ref/FinalReference  occurs when we try to run a Java Application from an android project

Fix:
1.Select the project.
2.Open the run configuration .
3.New configuration->Classpath tab->under Bootstrap entries->find android library->remove it
4.Now, select bootstrap entries->click on Advanced->select Add library->click ok.
5.Select the JRE-> finish
6.Run the project now.