Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8198713
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T06:00:33+00:00 2026-06-07T06:00:33+00:00

I previously asked a question with regards to parsing json, since then I found

  • 0

I previously asked a question with regards to parsing json, since then I found an example that should work correctly but its not. The code I now have is as followed:

  public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        final TextView tv = (TextView)findViewById(R.id.result);

            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);

            String username = "exampleusername";
            String password = "examplepassword";
            String url = "http://www.example.com/api/core/v1/my/";
            String unp = username+":"+password;


            class MyUser {
                  public String firstName;
                  public String lastName;
            }


            try {
                HttpClient client = new DefaultHttpClient();
                HttpGet header = new HttpGet(url);
                String encoded_login = Base64.encodeToString(unp.getBytes(), Base64.NO_WRAP);
                header.setHeader(new BasicHeader("Authorization", "Basic "+encoded_login));
                HttpResponse response = client.execute(header);
                HttpEntity entity = response.getEntity();

                BufferedReader reader = new BufferedReader(new InputStreamReader(entity.getContent()));
                for (String line; (line = reader.readLine()) != null;) {
                    if (line.isEmpty()) break; // Stop when headers are completed. We're not interested in all the HTML.
                    final String newjson = line.replaceAll("throw 'allowIllegalResourceCall is false.';", "");

                    JSONObject mainObject = new JSONObject(newjson);
                    JSONObject uniObject = mainObject.getJSONObject("firstName");

               // System.out.println(newjson + newjson.contains("firstName") + "," + newjson.contains("lastName"));
               System.out.println(uniObject);
             //   System.out.println(uniURL);
                }

With this code i am now getting the following error:

W/System.err(910): org.json.JSONException: End of input at character 0 of 
W/System.err(910): at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
W/System.err(910): at org.json.JSONTokener.nextValue(JSONTokener.java:97)
W/System.err(910): at org.json.JSONObject.<init>(JSONObject.java:154)
W/System.err(910): at org.json.JSONObject.<init>(JSONObject.java:171)
W/System.err(910): at basic.authentication.BasicAuthenticationActivity.onCreate(BasicAuthenticationActivity.java:84)
W/System.err(910): at android.app.Activity.performCreate(Activity.java:4465)
W/System.err(910): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
W/System.err(910): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
W/System.err(910): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
W/System.err(910): at android.app.ActivityThread.access$600(ActivityThread.java:123)
W/System.err(910): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
W/System.err(910): at android.os.Handler.dispatchMessage(Handler.java:99)
W/System.err(910): at android.os.Looper.loop(Looper.java:137)
W/System.err(910): at android.app.ActivityThread.main(ActivityThread.java:4424)
W/System.err(910): at java.lang.reflect.Method.invokeNative(Native Method)
W/System.err(910): at java.lang.reflect.Method.invoke(Method.java:511)
W/System.err(910): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
W/System.err(910): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
W/System.err(910): at dalvik.system.NativeStart.main(Native Method)

part of the json string that i am pulling down is as followed:

{
  "enabled" : true,
  "email" : "example@gmail.com",
  "firstName" : "example firstname",
  "lastName" : "example lastname",
  "name" : "example fullname",
  "level" : {

I am so so confused with json right now and why this is not working

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T06:00:34+00:00Added an answer on June 7, 2026 at 6:00 am

    try this read function

    public static JSONObject read(String url, String unp){
    
        System.out.println("Connecting to service URL : "+url);
        InputStream is = null;
        String result = "";
        // http post
        try {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(url);
            String encoded_login = Base64.encodeToString(unp.getBytes(), Base64.NO_WRAP);
            httppost.setHeader(new BasicHeader("Authorization", "Basic "+encoded_login));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        // convert response to string
        try {
            BufferedReader reader = new BufferedReader(new InputStreamReader(
                    is, "iso-8859-1"), 8);
            StringBuilder sb = new StringBuilder();
            String line = null;
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
    
        } catch (Exception e) {
            e.printStackTrace();
        }
    
        JSONObject json = null;
        try {
            json = new JSONObject(result);
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    
        return json;
    }
    
    }
    
    • here i connects to the service first
    • downloads the response
    • pass it for JSONObject creation, instead of doing it in loop
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Since I haven't found an answer to the question asked previously here I'm trying
I previously asked this question, which was answered, but someone gave a suggestion that
A similar question was previously asked, but none of the answers really provided what
I'm deleting a previously asked question and asking again more succinctly now that I
I have previously asked a question on AS3 and timing for games but got
I have previously asked two questions Question 1 and Question 2 but there is
I have seen this question asked previously but can not find a clear explanation
Ok, I have previously asked a similar question to this but it was voted
From this previously asked question , I have noticed that when I move a
I know this may looks like a previously asked question but I'm facing a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.