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.
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:
Making it the only entry in an array:
…which results in
["Success!"], which you can use like this in your Java code (since you say you’re using theorg.javalibrary):or of course
Or making it the only property of an object:
…which results in
{"result":"Success!"}, which you can use like this in your Java code:or of course
You could probably use
JSONTokenizerto read your current JSON fragment ("Success!"), but I wouldn’t suggest going that way. Stick to exchanging valid JSON documents.