2014-04-28 34 views
0

我已經使用MYSQL數據庫並使用PHP將它連接到我的Android應用程序。我在表格中添加了一個示例行,PHP腳本正在返回JSON。我已經盡力解析JSON並將其顯示在列表中。但它似乎並沒有工作。能夠運行應用程序 - 但不顯示內容 - 僅顯示對話框。它似乎在一個活動的情況下工作 - 應該做什麼改變才能使它適用於片段。當我打開片段時,這是我的LogCat - http://prntscr.com/3f6k0aJSON解析數據不在ListFragment中顯示

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瀏覽模塊。

回答

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; 
} 
+0

現在用的是同樣的JSON解析器代碼。 – Bhargav

+0

嘗試JsonObject和JSonArray ............ – Shreeshesh

+0

沒有幫助:( – Bhargav