简体   繁体   English

为什么我不能创建JSON对象?

[英]Why can't I create the JSON Object?

I'm using HTTP Get to retrieve a JSON formatted String from my website. 我正在使用HTTP Get从我的网站检索JSON格式的字符串。

However, when I try to create the JSONObject, nothing happens. 但是,当我尝试创建JSONObject时,没有任何反应。 The object isn't created and I can't acces Arrays or anything inside it. 该对象未创建,并且我无法访问Array或其中的任何内容。

I've checked that my app does get the right String and it is correctly formatted as a JSON. 我检查了我的应用程序是否获取正确的String并将其正确格式化为JSON。

Do I have to declare it in some particular way or have separate try/catch blocks...? 我是否必须以某种特定方式声明它或具有单独的try / catch块...?

I'm at my wits end with this, so any help would be most appreciated... 我为此尽力而为,所以任何帮助将不胜感激...

Lower you have all the code I've written in this issue and although it's not much, I'm stuck and don't know where to go with this... 降低您拥有我在本期中编写的所有代码的范围,尽管虽然数量不多,但我受困于此并且不知道该去哪儿...

public String result, testString;
public TextView resultText; 
public String url = "http://www.ace.ucv.ro/android/android.php";

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainmenu);

    resultText = (TextView) findViewById(R.id.result);

    try {
        HttpGet httpGet = new HttpGet(url);

        result = EntityUtils.toString(new DefaultHttpClient().execute(httpGet).getEntity());

        JSONObject jsonObject = new JSONObject(result);

    } catch (ParseException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }           
    resultText.setText(result);

}

The URL you have in code (http://www.ace.ucv.ro/android/android.php) does not return Json object, instead it is a JSONArray. 您在代码中拥有的URL(http://www.ace.ucv.ro/android/android.php)不会返回Json对象,而是一个JSONArray。

Just replace JSONObject with JSONArray and you'll be fine. 只需将JSONObject替换为JSONArray就可以了。

Are you sure that the string stored in result is just a JSONObject, or is it a JSONArray? 您确定结果中存储的字符串只是一个JSONObject,还是JSONArray?

I remember stumbling on a similar problem. 我记得遇到类似的问题。

At first glance, your code looks familiar / reasonable. 乍一看,您的代码看起来很熟悉/合理。 But perhaps you should un-nest it and log the intermediate results - which would help you see which stage is going wrong. 但是也许您应该取消嵌套并记录中间结果-这将帮助您查看哪个阶段出了问题。 For example... 例如...

HttpGet httpGet = new HttpGet(url);
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpResponse response = httpClient.execute(httpGet);
Log.v("test", "HTTP response = " + response);
HttpEntity entity = response.getEntity();
Log.v("test", "HTTP entity = " + entity);
String content = EntityUtils.toString(entity);
Log.v("test", "HTTP content = " + content);
JSONObject mJsonObj = new JSONObject(mContent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM