返回JSON和在Java中,Android的解析 [英] return JSON and parse in java, android
问题描述
我从PHP返回一个JSON字符串:
< PHP
$结果=阵列(
结果=> 成功,
用户名= GT; 一些用户名,
工程=> 其他一些价值
);回声json_en code($结果);
?>
我发现了一个Java示例网上的作品。它使用StringBuilder的输出使用吐司的响应。我想实际解析它作为一个JSON对象,所以我可以参考每个键=>值,但不知道如何去做。这是我使用的例子:
私人无效tryLogin(字符串usernameInput,字符串passwordInput)
{
HttpURLConnection的连接;
OutputStreamWriter请求= NULL;
网址URL = NULL;
串响应=无效;
字符串参数=用户名=+ usernameInput +&放大器;密码=+ passwordInput; 尝试
{
URL =新的URL(的getString(R.string.loginLocation));
连接=(HttpURLConnection类)url.openConnection();
connection.setDoOutput(真);
connection.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);
connection.setRequestMethod(POST); 请求=新OutputStreamWriter(connection.getOutputStream());
request.write(参数);
request.flush();
request.close();
串线=; InputStreamReader的ISR =新的InputStreamReader(connection.getInputStream());
读者的BufferedReader =新的BufferedReader(ISR);
StringBuilder的SB =新的StringBuilder();
而((行= reader.readLine())!= NULL)
{
sb.append(行+\\ n);
}
响应= sb.toString(); Toast.makeText(这一点,从服务器消息:\\ n+响应,0).show();
isr.close();
reader.close();
}
赶上(IOException异常E)
{
Log.i(NetworkTest,网络错误:+ E);
}
}
这是什么code当前返回:
05-04 19:19:54.724:信息/ NetworkTest(1061):{结果:成功,用户名:rondog,项目: 1,2}
只是要清楚,我是pretty确信我知道如何解析字符串。我所困惑的是得到响应从服务器返回,推动了以JSONObject的(或者是'反应',我传递对象?)。任何帮助是AP preciated,谢谢!
(或者是'反应',我传递对象?)
块引用>是的,是的。该公司预计它的构造函数的字符串对象来分析它。
I am returning a JSON string from PHP:
<?php $results = array( "result" => "success", "username" => "some username", "projects" => "some other value" ); echo json_encode($results); ?>
I found a java example online that works. It uses StringBuilder and outputs the response using Toast. I want to actually parse it as a JSON object so I can reference each key=>value, but not sure how to do it. This is the example I am using:
private void tryLogin(String usernameInput, String passwordInput) { HttpURLConnection connection; OutputStreamWriter request = null; URL url = null; String response = null; String parameters = "username=" + usernameInput + "&password=" + passwordInput; try { url = new URL(getString(R.string.loginLocation)); connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestMethod("POST"); request = new OutputStreamWriter(connection.getOutputStream()); request.write(parameters); request.flush(); request.close(); String line = ""; InputStreamReader isr = new InputStreamReader(connection.getInputStream()); BufferedReader reader = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } response = sb.toString(); Toast.makeText(this, "Message from server: \n" + response, 0).show(); isr.close(); reader.close(); } catch(IOException e) { Log.i("NetworkTest","Network Error: " + e); } }
This is what the code currently returns:
05-04 19:19:54.724: INFO/NetworkTest(1061): {"result":"success","username":"rondog","projects":"1,2"}
Just to be clear, I am pretty sure I know how to parse the string. What I am confused on is getting the response back from the server and pushing that to the JSONObject (or is 'response' the object that I pass?). Any help is appreciated, thanks!
解决方案(or is 'response' the object that I pass?)
Yes, it is. It expects a string object in it's constructor to parse it.
这篇关于返回JSON和在Java中,Android的解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!