0

我使用了 MYSQL 数据库并使用 PHP 将其连接到我的 Android 应用程序。我在表中添加了一个示例行,PHP 脚本正在返回 JSON。我已尽力解析 JSON 并将其显示在列表中。但它似乎不起作用。能够运行应用程序 - 但不显示内容 - 仅显示对话框。它似乎适用于活动情况 - 应该进行哪些更改以使其适用于片段。这是我打开片段时的 LogCat - http://prntscr.com/3f6k0a

package com.example.socbeta;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.ListFragment;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

public class events extends ListFragment {

private ProgressDialog pDialog;

 private static final String READ_EVENTS_URL ="http://socapptest.comoj.com/socbeta/events.php";   

//JSON IDS:
private static final String TAG_SUCCESS = "success";
private static final String TAG_EventName = "EventName";
private static final String TAG_POSTS = "posts";
private static final String TAG_ID = "ID";
private static final String TAG_DATE = "Date";
private static final String TAG_MESSAGE = "message";

private JSONArray mEvents = null;
private ArrayList<HashMap<String, String>> mEventList;


public events(){}

@Override public View onCreateView(LayoutInflater inflater, ViewGroup container,  Bundle savedInstanceState) 
{ 
  return inflater.inflate(R.layout.events, container, false); 
}



@Override
public void onResume() {
    // TODO Auto-generated method stub
    super.onResume();
    //loading the comments via AsyncTask
    new LoadEvents().execute();

  }
  public void updateJSONdata() {





    mEventList = new ArrayList<HashMap<String, String>>();


    JSONParser jParser = new JSONParser();

    JSONObject json = jParser.getJSONFromUrl(READ_EVENTS_URL);


    try {


        mEvents = json.getJSONArray(TAG_POSTS);


        for (int i = 0; i < mEvents.length(); i++) {
            JSONObject c = mEvents.getJSONObject(i);

            String EventName = c.getString(TAG_EventName);
            String content = c.getString(TAG_MESSAGE);
            String Date = c.getString(TAG_DATE);


            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            map.put(TAG_EventName, EventName);
            map.put(TAG_MESSAGE, content);
            map.put(TAG_DATE, Date);


            mEventList.add(map);


        }

    } catch (JSONException e) {
        e.printStackTrace();
    }
}
private void updateList() {


ListAdapter adapter = new SimpleAdapter(getActivity(), mEventList,
        R.layout.list_item, 
        new String[] { TAG_EventName, TAG_MESSAGE,TAG_DATE }, 
        new int[] { R.id.eventname, R.id.message,R.id.date });



            setListAdapter(adapter);


            ListView lv = getListView();    
            lv.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {

            });
}

public class LoadEvents extends AsyncTask<Void, Void, Boolean> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(getActivity());
        pDialog.setMessage("Loading Events...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(true);
        pDialog.show();
    }
    @Override
    protected Boolean doInBackground(Void... arg0) {

        updateJSONdata();
        return null;

    }


    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);
        pDialog.dismiss();

        updateList();
        }
      }
   }

编辑 :

让我的代码工作。问题出在 JSON 结构上。我没有正确导航,这给了我“消息无价值”错误。对于可能遇到相同问题的其他人 - 请记住,当您的 JSON 中有 { 时,您使用 JSONArray,而当您有 [ 时,您使用 JSONObject。您需要通过 JSON 明智地导航模块。

4

1 回答 1

1

试试这个代码

将 json 字符串传递给此方法。它会起作用的

公共 JSONObject getJSONfromURL(字符串 url)

{
    InputStream is = null;
    JSONObject jObj = null;
    String json = "";

尝试 {

// defaultHttpClient
   DefaultHttpClient httpClient = new DefaultHttpClient();
   HttpGet httpget= new HttpGet(url);
   HttpResponse httpResponse = httpClient.execute(httpget);
   HttpEntity httpEntity = httpResponse.getEntity();
        is = httpEntity.getContent();

    } catch (Exception e) {
        e.printStackTrace();
    }

    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"), 8);
        StringBuilder sb = new StringBuilder();
        String line = null;
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
        is.close();
        json = sb.toString();
    } catch (Exception e) {
        }

    try {
        jObj = new JSONObject(json);
    } catch (JSONException e) {
        Log.e("JSON Parser", "Error parsing data " + e.toString());
    }

    return jObj;
}
于 2014-04-28T10:55:24.660 回答