2

I am a beginner in Java and PHP.

I made a client Java app that uses webView to Pass a string to php client page which will insert this string to the DB.

my question is, how can I return a false value to the java client app in case the PHP failed to insert the string to the DB using webView.

the php code will be something like this:

 if(Insertion suceed)
{return true;}  

else {return false;}

but I dont know how to make it in the Java side.

P.S: I need the return value to display an alert to the java user to restart the app.

user1494142
  • 131
  • 1
  • 2
  • 9

3 Answers3

2

You could use PHP/Java Bridge to allow communication between your applications. You could also set up a REST service provided by PHP (see this previous SO question) and allow your Java application to consume that service. The PHP application just sends true/false as a response.

Community
  • 1
  • 1
npinti
  • 51,780
  • 5
  • 72
  • 96
2

You may try to communicate from php to java using get/post request. Communication the other way can be done by viewing xml or json response. Json is quite nice to answer and supported by php (php object may be parsed into json using json_encode()/json_decode() Java also supports json communication (see http://www.json.org/java/ ). Json allows sending null.

See more here http://www.json.org/

if all your php outputs/writes is the status of your insertion than code may look like

if(Insertion suceed){
   echo "true";
}else {
   echo "false:;
}

in java you need to capture the content of your Web view. i found simmilar problem and possible solution here How do I get the web page contents from a WebView?. if it also outputs other data/web content than you just need to "hide" your status output somewhere and cut it out from output source string in java

now if you have your content in String value just use library located here: http://code.google.com/p/json-simple/

your java code stould look like this (in my case "response" is the string with content):

String response = "false";
JSONParser parser = new JSONParser();
Boolean result = (Boolean)parser.parse(response);

i hope you understood

Community
  • 1
  • 1
Misiakw
  • 902
  • 8
  • 28
  • Thank you for your response but I tried json several times but never succeeded, maybe it is advanced for me as a beginner. Besides, we are forced to use the webView for passing the variable from client to server – user1494142 Oct 02 '12 at 10:24
  • can you acces data response from php i'll try to manage some classes after my work, and i'll insert some code here. we stay in contact – Misiakw Oct 02 '12 at 10:29
  • I do not under stand what you meant by "can you acces data response from php", but I will defiantly be here waiting for your response. Thank you for your help! – user1494142 Oct 02 '12 at 10:42
  • I've edited my solution with some code i promised. hope you will understand – Misiakw Oct 03 '12 at 08:08
  • Thank you so much for your time and effort! Unfortunately I was not able to use your help. I had to submit the partial work of the project before the time you posted it. But I will definitely check it out. – user1494142 Oct 06 '12 at 06:29
0

You can do that by setting a Boolean variable=to the webView call in the java app and made the php function returns true or false to the Java app.

I am not able to check if this is working or not since the php code for the server is not uploaded until 3 weeks!

user1494142
  • 131
  • 1
  • 2
  • 9