dqyin0101 2015-09-15 03:45
浏览 54

否JsonResponse即使单个数据为空也获得

  {"futsal_id":"59",
    "description":[{
    "futsal_id":"59",
    "futsal_desc":"This is great futsal"}],

    "features":[{"futsal_id":"59","futsal_feat":"free 4 bottles of water"}],
    "dimension":null,
    "no_of_futsal":null,
    "opening_hrs":null,
    "price_weekdays_price1":[{
            "futsal_id":"59",
            "price_id":"1",
            "start_time":"6am",
            "end_time":"10am",
            "price":"1000"}],
    "price_weekdays_price2":[{
            "futsal_id":"59",
            "price_id":"2",
            "start_time":"10am",
            "end_time":"3pm",
            "price":"1200"}],
    "price_weekdays_price3":[{
            "futsal_id":"59",
            "price_id":"3",
            "start_time":"3pm",
            "end_time":"9pm",
            "price":"1300"}],
    "price_weekend_price1":null,
            "price_weekend_price2":null,
            "price_weekend_price3":null,
    "images":[],
    "image_count":0,
    "news":null}

This is My JsonResponse from PHP. I cannot get any of this response in android studio with these response because of present null data in these response. If no null data are present, response.getString() has all these data but response.getString has no response because of null data. What might be the problem?

    class ShowResult extends AsyncTask<Void, Void, String[]> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pd.show();
    }

    @Override
    protected void onPostExecute(String[] aVoid) {
        super.onPostExecute(aVoid);
        pd.cancel();
        txt.setText(result[0]);
      }

    @Override
    protected String[] doInBackground(Void... params) {
        try {
            Log.d("sssssssssssssss", "sadf");

            URL url = new URL("http://futsalgroove.s4generation.com/app/android_c/show_details/");
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setDoOutput(true);
            urlConnection.setRequestMethod("POST");

            String urlParameters = "id=" + bundle.getString("id");
            //sending the parameter using DataOutputStream
            DataOutputStream wr = new DataOutputStream(urlConnection.getOutputStream());
            wr.writeBytes(urlParameters);
            wr.flush();
            wr.close();

            //Reading the data or response from the PHP file
            BufferedReader in = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));

            Log.d("sssssssssssssss", "iii" + in);
            String inputLine;
            StringBuffer response = new StringBuffer();

            while ((inputLine = in.readLine()) != null) {
                response.append(inputLine);
                Log.d("Detail Outputss", inputLine);
            }
            in.close();
            //Using the JasonObject from php
            Log.d("Detail Outputsss", "" + response.toString());

            JSONObject json = new JSONObject(response.toString());

            JSONArray description = json.getJSONArray("description");
            JSONObject descObj = description.getJSONObject(0);

            result[0] =  descObj.getString("futsal_desc");
            Log.d("DetailOut","" + result[0]);
     }

       } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {

        }
        return result;
    }
}
  • 写回答

1条回答 默认 最新

  • dqb78642 2015-09-15 06:33
    关注

    Replace your code

    JSONObject json = new JSONObject(response.toString());
    JSONArray description = json.getJSONArray("description");
    JSONObject descObj = description.getJSONObject(0);
    result[0] =  descObj.getString("futsal_desc");
    Log.d("DetailOut","" + result[0]);
    

    with

    JObject json = JObject.Parse(response.toString());
    JArray description = (JArray)json["description"];
    JObject descObj = (JObject)description[0];
    result[0] = descObj["futsal_desc"].ToString();
    Log.d("DetailOut","" + result[0]);
    

    may be this will work. for JObject use Newtonsoft.Json.Linq on top

    using Newtonsoft.Json.Linq;
    
    评论

报告相同问题?

悬赏问题

  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 Macbookpro 连接热点正常上网,连接不了Wi-Fi。
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程