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 6662909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T02:26:29+00:00 2026-05-26T02:26:29+00:00

I want to insert some items into the database. In the main activity, I

  • 0

I want to insert some items into the database. In the main activity, I retrieve information from the user and pass it to the another class to do some parsing. My JSONObject keeps showing up as NULL.

I am sorry if I am not clear with the question . I’ve tried to explain it as much as possible.

Below is the code your inputs are welcome

public class MainActivity extends Activity {
/** THE FOLLOWING STRINGS WILL BE DISPLAYED IN LOGCAT */

final String TAG = "########-------MAIN ACTIVITY: LOGIN--------######";
final String URL = "http://46.51.193.32/timereport/ses/sessions";
UserHelper userAdapter;
UserHelper db;
EditText edit_password,edit_username,edit_company;
String regName;
int duration = Toast.LENGTH_LONG;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    db = new UserHelper(this);
    userAdapter = new UserHelper(this);
     edit_password = (EditText)findViewById(R.id.password);
     edit_username = (EditText)findViewById(R.id.user_name);
     edit_company = (EditText)findViewById(R.id.company_string);
    Button login = (Button)findViewById(R.id.login_button);
    login.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
           JSONObject jsonobj = new JSONObject();
            try{

                JSONObject subJson = new JSONObject();
                subJson.put("username", edit_username.getText().toString());
                subJson.put("password", edit_password.getText().toString());
                subJson.put("company", edit_company.getText().toString());
                jsonobj.put("user", subJson);
            }
            catch(JSONException e) {
                Log.i("","#####-----error at catch jsonexception-----#####");

            }
HandleJSON.SendHttpPost(URL, jsonobj);
                String regNameSplit[] = regName.split("-");
            try{
                userAdapter.openDatabase();
                long id = db.insertIntoDatabase(edit_username.getText().toString(),edit_company.getText().toString(), edit_password.getText().toString(),regNameSplit[0], regNameSplit[2]);
                Toast.makeText(getApplicationContext(), "You have successfully logged in as: " +"\n" +regNameSplit[0], duration).show();
                Log.i(TAG, "Printing value of id which will be inserted only to remove warnings "+id);
                userAdapter.closeDatabase();
            }
            catch(SQLiteException e){
                e.printStackTrace();
            } 
        }
    });
}

}

This is the class to which I am sending the JSON object to be parsed

public class HandleJSON{

UserHelper userAdapter;
private static final String TAG = "&&----HTTPClient-----**";
public static String SendHttpPost (String URL, JSONObject jsonobj) {
String regName = "";

try{    

        Log.v("Json object request is ",jsonobj.toString());
        DefaultHttpClient httpClientInstance = GetHttpClient.getHttpClientInstance();
        HttpPost httpPostRequest = new HttpPost(URL);
        Log.v(TAG,"The url is "+URL);

        StringEntity se;
        se = new StringEntity(jsonobj.toString());

        httpPostRequest.setEntity(se);
        httpPostRequest.setHeader("Accept", "application/json");
        httpPostRequest.setHeader("Content-type", "application/json");

        long t = System.currentTimeMillis();
        HttpResponse response = (HttpResponse) httpClientInstance.execute(httpPostRequest);
        Log.i(TAG, "HTTPRESPONSE RECIEVED" +(System.currentTimeMillis()-t) + "ms");

            String resultString = convertStreamToString(response.getEntity().getContent());
            Log.v(TAG , "The response is " +resultString);
            JSONObject jsonObj = new JSONObject(resultString);
            JSONObject sessionJson = jsonObj.getJSONObject("session");
            String sessionId = sessionJson.getString("sessionid");
            String name = sessionJson.getString("name");
            Log.v(TAG,"The session ID is "+sessionId);
            Log.v(TAG,"The name is "+name);
            regName = name+"-"+sessionId+"-"+URL;

} catch (Exception e){
    e.printStackTrace();
}
return regName;
}

 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();
 }
 }

I’ve just added some of the code that was missing at the MainActivity,

String regNameSplit[] = regName.split("-");

keeps showing up as null

  • 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-05-26T02:26:29+00:00Added an answer on May 26, 2026 at 2:26 am

    I don’t see anything wrong with this, could you tell me what is the use of regname ?

    at your mainactivity just change the following:

    regname = HandleJSON.SendHttpPost(URL, jsonobj);
    

    Your not calling back regname to be assigned to name and sessionid that you are returning at the sendhttppost.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to use javascript to insert some elements into the current page. Such
I want to insert say 50,000 records into sql server database 2000 at a
I have lists of about 20,000 items that I want to insert into a
I have a page and I want to insert 'Some text' on that page,
After creating a table (by migration), I want to insert some entries directly. How
I want to insert and delete some chars in the middle of a file.
I want to insert the following as the value for a variable in some
We're currently doing some Word automation, and want to be able to insert a
Use case: I've just entered insert mode, and typed some text. Now I want
I want to do some quick inserts but avoid duplicates into a Table. For

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.