0

My String result = {"userName":"balaji@tispl.in","authenticationResult":true,"errorMessage":"No errors."}

I want to Parse this String using json.simple & convert into a json object.
My code is as below:

package in.tispl.phoenix8;
    
    import org.json.JSONException;
    import org.json.JSONObject;
    import org.json.simple.parser.JSONParser;
    import org.json.simple.parser.ParseException;
    
    import java.io.FileNotFoundException;
    import java.io.FileReader;
    import java.io.IOException;
    
    public class Client {
        
        public static void main(String[] args) {
            HttpReq httpr = new HttpReq();
            httpr.setRequrl("http://www.crogress.com:8080/crogress/authenticateUser?userName=balaji@tispl.in&password=xxxxx");
            String result = httpr.getrequest();
            System.out.println(result);
            JSONObject jsonObject = createJSONObject(result);
            System.out.println(jsonObject);
        }
    
        private static JSONObject createJSONObject(String jsonString){
            JSONObject  jsonObject=new JSONObject();
            JSONParser jsonParser=new  JSONParser();
            if ((jsonString != null) && !(jsonString.isEmpty())) {
                try {
                    jsonObject=(JSONObject) jsonParser.parse(jsonString);
                } catch (org.json.simple.parser.ParseException e) {
                    e.printStackTrace();
                }
            }
            return jsonObject;
        }
    
    }

I am getting the below Error:

"C:\Program Files\Android\Android Studio\jre\bin\java" -Didea.launcher.port=56311 "-Didea.launcher.bin.path=C:\Program Files\Android\Android Studio\bin" -Dfile.encoding=UTF-8 -classpath "C:\Users\Admin\AppData\Local\Android\Sdk\platforms\android-26\android.jar;C:\Users\Admin\AppData\Local\Android\Sdk\platforms\android-26\data\res;C:\Users\Admin\AndroidStudioProjects\Phoenix8\app\build\intermediates\classes\debug;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\support-media-compat-26.1.0.aar\5757022abd6a5b2c555fcf939b3a4042\jars\classes.jar;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\support-media-compat-26.1.0.aar\5757022abd6a5b2c555fcf939b3a4042\res;C:\Users\Admin\AndroidStudioProjects\Phoenix8\app\libs\json-simple-1.1.1.jar;C:\Users\Admin\.gradle\caches\modules-2\files-2.1\android.arch.lifecycle\common\1.0.0\e414a4cb28434e25c4f6aa71426eb20cf4874ae9\common-1.0.0.jar;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\support-fragment-26.1.0.aar\75e99ed55ce9e9342e6d4a1dcc9d7d14\jars\classes.jar;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\support-vector-drawable-26.1.0.aar\45df467a57dbd94d25051052a8116512\jars\classes.jar;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\runtime-1.0.0.aar\a34124cf5f12680d5ecf738266959184\jars\classes.jar;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.1.0.aar\16329d99170c8dfe7b68a2ef302c9114\res;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\appcompat-v7-26.1.0.aar\16329d99170c8dfe7b68a2ef302c9114\jars\classes.jar;C:\Users\Admin\.gradle\caches\modules-2\files-2.1\com.android.support\support-annotations\26.1.0\814258103cf26a15fcc26ecce35f5b7d24b73f8\support-annotations-26.1.0.jar;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\support-core-utils-26.1.0.aar\fb398e1b1f5019acfc07ed1ad53f85ad\jars\classes.jar;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\support-core-ui-26.1.0.aar\99d19a20b94d440b54fe6e524437352f\jars\classes.jar;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.3.aar\eaad1d3f78dbc607549d67cb06dc7279\res;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\constraint-layout-1.1.3.aar\eaad1d3f78dbc607549d67cb06dc7279\jars\classes.jar;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\support-compat-26.1.0.aar\c117f9c895b57f767bd20ab90b13fae7\res;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\support-compat-26.1.0.aar\c117f9c895b57f767bd20ab90b13fae7\jars\classes.jar;C:\Users\Admin\.gradle\caches\modules-2\files-2.1\android.arch.core\common\1.0.0\a2d487452376193fc8c103dd2b9bd5f2b1b44563\common-1.0.0.jar;C:\Users\Admin\.gradle\caches\modules-2\files-2.1\com.android.support.constraint\constraint-layout-solver\1.1.3\bde0667d7414c16ed62d3cfe993cff7f9d732373\constraint-layout-solver-1.1.3.jar;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\support-v4-26.1.0.aar\89bc8501bf07eefdf9a75408ba6acef1\jars\classes.jar;C:\Users\Admin\.gradle\caches\transforms-1\files-1.1\animated-vector-drawable-26.1.0.aar\488e0b1d1d386e3e31adae1bb27bc541\jars\classes.jar;C:\Program Files\Android\Android Studio\lib\idea_rt.jar" com.intellij.rt.execution.application.AppMainV2 in.tispl.phoenix8.Client
    Exception in thread "main" java.lang.RuntimeException: Stub!
        at org.json.JSONObject.<init>(JSONObject.java:20)
        at in.tispl.phoenix8.Client.createJSONObject(Client.java:41)
        at in.tispl.phoenix8.Client.main(Client.java:19)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at com.intellij.rt.execution.application.AppMainV2.main(AppMainV2.java:131)

**Please Help!**
  • 1
    After the edit it looks better, but a very important part is removed: the reference to android.jar. Android SDK libraries contain "empty" code, they allow the compiler to compile code, but they are not meant for running the result. You either need to create a proper Android app for testing and run it on a phone or in the emulator, or just get a JSON library meant for desktop use. See a list of options on https://www.json.org Side remark: in the context of programming, ***stub*** is a common name for incomplete/placeholder entites. – tevemadar Sep 13 '20 at 16:58
  • 1
    Added that line (android.jar) back into the error; for others reference. Thank you for pinpointing the issue. I tried the code without the android.jar (in eclipse). It works well. Now, I have to find a way to make it work in Android Studio. Let me try to put the same code in main activity and try instead of the main function. – Balaji Subashchandran Sep 13 '20 at 17:27
  • I just identified the error!!! I have made the wrong imports. I have imported "import org.json.JSONObject;" instead of "import org.json.simple.JSONObject;" That made all the difference. Now it works in the android studio also!!! :) – Balaji Subashchandran Sep 13 '20 at 17:39

0 Answers0