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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:29:46+00:00 2026-06-13T12:29:46+00:00

I have a Java application communicating with a SQL database via a PHP script,

  • 0

I have a Java application communicating with a SQL database via a PHP script, using JSON. I have used JSON before to json_encode an array, that works fine, you can call the values in Java by using the key of each entry. But now, I json_encoded a string in PHP, like this:
json_encode("Succes!");
Now, how do I request that value in the Java side of business? I need something to put into jsonResponse.getString('key'); as a key. What is the key…

I hope you understand my question…

public String send(String username, String password, String database){
    //Create a HTTPClient as the form container
    HttpClient httpclient;

//Use HTTP POST method
HttpPost httppost;

//Create an array list for the input data to be sent
ArrayList<NameValuePair> nameValuePairs;

//Create a HTTP Response and HTTP Entity
HttpResponse response;
HttpEntity entity;

//Create new default HTTPClient
httpclient = new DefaultHttpClient();

//Create new HTTP POST with URL to php file as parameter
httppost = new HttpPost("http://192.168.144.150/login/add.php");

String pass = md5(password);

//Next block of code needs to be surrounded by try/catch block for it to work
try {
    //Create new Array List
    nameValuePairs = new ArrayList<NameValuePair>();

    //place them in an array list
    nameValuePairs.add(new BasicNameValuePair("username", username));
    nameValuePairs.add(new BasicNameValuePair("password", pass));
    nameValuePairs.add(new BasicNameValuePair("database", database));

    //Add array list to http post
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    //assign executed form container to response
    response = httpclient.execute(httppost);

    //check status code, need to check status code 200
    if(response.getStatusLine().getStatusCode()== 200){

        //assign response entity to http entity
        entity = response.getEntity();

        //check if entity is not null
        if(entity != null){


            //Create new input stream with received data assigned
            InputStream instream = entity.getContent();

            //Create new JSON Object. assign converted data as parameter.
            //JSONObject jsonResponse = new JSONObject(convertStreamToString(instream));
            JSONArray a = new JSONArray(convertStreamToString(instream));

            //assign json responses to local strings
            String res = a.getString(0);

            //Return the json response to the gui
            return res;

        } else {
            //Toast.makeText(this, "kapot", Toast.LENGTH_SHORT).show();
            return new String("De entiteit is leeg. Kortom: kapot");
        }


    } else {
        //Toast.makeText(this, "statuscode was niet 200", Toast.LENGTH_SHORT).show();
        return new String("De statuscode was niet 200.");
    }


} catch(Exception e){
    e.printStackTrace();
    //Display toast when there is a connection error
    //Change message to something more friendly
    //Toast.makeText(this, "verbindingsproblemem", Toast.LENGTH_SHORT).show();
    return new String("Er is een fout opgetreden. Controleer a.u.b. uw gegevens en de internetverbinding.");
}

Don’t mind the Dutch parts, it’s unimportant.

  • 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-13T12:29:47+00:00Added an answer on June 13, 2026 at 12:29 pm

    The output of json_encode("Success!") is "Success", which isn’t in and of itself a valid JSON document. A valid JSON document’s top-level entity (the root) must be an object or an array.

    If you want to just return a single string, you might do that by:

    1. Making it the only entry in an array:

      json_encode(array("Success!"))
      

      …which results in ["Success!"], which you can use like this in your Java code (since you say you’re using the org.java library):

      JSONArray a = new JSONArray(jsonStringFromPHP);
      String s = a.getString(0);
      

      or of course

      String s = new JSONArray(jsonStringFromPHP).getString(0);
      
    2. Or making it the only property of an object:

      json_encode(array('result' => "Success!")
      

      …which results in {"result":"Success!"}, which you can use like this in your Java code:

      JSONObject o = new JSONObject(jsonStringFromPHP);
      String s = o.getString("result");
      

    or of course

        String s = new JSONObject(jsonStringFromPHP).getString("result");
    

    You could probably use JSONTokenizer to read your current JSON fragment ("Success!"), but I wouldn’t suggest going that way. Stick to exchanging valid JSON documents.

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

Sidebar

Related Questions

I have a database server communicating with a Java application server using JDBC. I
I have a java-application using JDBC for database interaction. I want to do a
I've got a database that is mirrored using SQL 2008 Mirroring. I have a
I am using sql server 2008 which is communicating to java web service which
I have used the Java Jacob library to communicate with inedesign server application which
I have a client-server application written in Java using CORBA for the communication. The
I have a java plugin to an application that I'm using (I didn't write
I have Java application which sends pointer to function (callback) to some native dll
I have a Java application that run as a background service, i.e. no GUI.
I have a java application where main class is dependent with some other classes.

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.