从PHP返回值的Java客户端 [英] Return Value to the Java client from the PHP

查看:118
本文介绍了从PHP返回值的Java客户端的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Java和PHP初学者。

I am a beginner in Java and PHP.

我做了一个使用web视图来传递一个字符串到PHP这将插入这个字符串的数据库客户端网页客户端的Java应用程序。

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.

我的问题是,我怎么能返回一个错误值的情况下,Java客户端应用程序的PHP未能插入字符串到数据库使用web视图。

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.

在PHP code将是这样的:

the php code will be something like this:

 if(Insertion suceed)
{return true;}  

else {return false;}

但我不知道如何使它在Java的一面。

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

P.S:我需要的返回值来显示一个警告到Java用户重新启动应用程序

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

推荐答案

您可以尝试使用GET / POST请求从PHP沟通去渣。通信的其他方式可以通过查看XML或JSON响应来实现。 JSON是相当不错的回答和PHP(PHP对象可以使用json_en code()/ json_de code()的Java也支持JSON通讯(见的 http://www.json.org/java/ )。JSON允许在发送空。

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.

查看更多 http://www.json.org/

如果您所有的PHP输出/写是你的插入比code的状态可能看起来像

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:;
}

在Java中你需要捕获你的Web视图的内容。我发现simmilar问题和可能的解决方案在这里
<一href=\"http://stackoverflow.com/questions/2376471/how-do-i-get-the-web-page-contents-from-a-webview\">How我从web视图获取网页内容?。如果还输出其它数据/网页内容比你只需要隐藏你的状态输出的地方,并在Java中的输出源字符串削减它

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

现在,如果你有只使用图书馆就设在这里的字符串值内容:
HTTP://$c$c.google.com/p/json-simple/

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

你的java code stould看起来像这样(在我的情况回应,是与内容字符串):

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

这篇关于从PHP返回值的Java客户端的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆