java访问php返回值,从PHP返回值的Java客户端

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.

解决方案

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.

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值