JSON无法从PHP的int值 [英] JSON can't get Int value from php
问题描述
我的问题很简单。我不能让项目从PHP和值在我的Android应用程序崩溃。我的PHP:
My problem is simple. I can't get the value of item from php and my android app crashes. My PHP:
<?php
$control["value"] = 0;
echo json_encode($control);
?>
我的Java的Android:
My Java Android:
private static final String CONTROL_URL = "xxxxx.controller.php";
int control;
...
@Override
public void onClick(View v) {
JSONParser jParser = new JSONParser();
//it crash here
JSONObject json = jParser.getJSONFromUrl(CONTROL_URL);
try {
control = json.getInt(CONTROL_URL);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (control == 0) {
//Do something...
}
当我试图去上我的浏览器的PHP文件它显示我:{值:0}。因此,能不能有人帮我解决这个问题?顺便说一句,我的网址 - xxxxx.controller.php是不是真实的,我用一个真实的我的应用程序,但我不想表现出来。
When i tried to go to my php file on browser it shows me: {"value":0} . So could someone help me solve this problem? By the way, my url - xxxxx.controller.php isn't real, I use a real one for my app, but I don't want to show it.
推荐答案
错误日志显示你正在主线程(NetworkOnMainThreadException),它的Android禁止在网络电话。您将有移动电话获取的JSON到一个单独的线程。使用一个异步任务会做到这一点。
your error log shows that you are making a network call on the main thread (NetworkOnMainThreadException), which android prohibits. You will have to move the call for getting the JSON to a separate thread. Using an Async Task will accomplish this.
这篇关于JSON无法从PHP的int值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!