2014-10-02 49 views
1

我正在與Json合作。我的JSON看起來是這樣的結構爲什麼我在分析我的json響應時看到最後一項?

this is a my json

我使用的AsyncTask解析的json,並顯示在列表視圖中我有一個問題信息jsonarray。我也解析了時間jsonobject,但在arraylist中只添加了最後一個對象。這是我的源代碼

private class CustomerStatistic extends AsyncTask<String, Void, String> { 

    ProgressDialog pDialog; 

    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     pDialog = new ProgressDialog(getActivity()); 

     pDialog.setCancelable(false); 
     pDialog.show(); 
     pDialog.setContentView(R.layout.custom_progressdialog); 
    } 

    @Override 
    protected String doInBackground(String... params) { 

     return Utils.getJSONString(params[0]); 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     super.onPostExecute(result); 

     try { 

      JSONArray mainJson = new JSONArray(result); 

      for (int i = 0; i < mainJson.length(); i++) { 
       JSONObject objJson = mainJson.getJSONObject(i); 

       objItem = new ServerItems(); 

       objItem.setImage(imageurl + objJson.getString("ipone_4")); 
       objItem.setTitle(objJson.getString("title")); 
       objItem.setYoutube(objJson.getString("youtube")); 
       objItem.setWritten(objJson.getString("written")); 
       objItem.setCategory(objJson.getString("category")); 
       objItem.setDescraption(objJson.getString("descraption")); 
       objItem.setGeorgia_time(objJson.getString("georgia_time")); 
       objItem.setWold_time(objJson.getString("wold_time")); 
       objItem.setStars(objJson.getString("stars")); 
       objItem.setBlurimage(imageurl 
         + objJson.getString("ipone_4_blur")); 

       JSONObject cinema = objJson.getJSONObject("Cinemas"); 
       JSONArray cinemasarray = cinema.getJSONArray("Cinemaname"); 

       for (int j = 0; j < cinemasarray.length(); j++) { 
        JSONObject objJson1 = cinemasarray.getJSONObject(j); 

        JSONArray info = objJson1.getJSONArray("info"); 
        for (int k = 0; k < info.length(); k++) { 
         JSONObject information = info.getJSONObject(k); 



         objItem.setTime(information.getString("time")); 
         Log.e("time is", objItem.getTime()); 

        } 

       } 

       arrayOfList.add(objItem); 

      } 

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

     if (pDialog != null) { 
      pDialog.dismiss(); 

      pDialog = null; 
     } 
     setAdapterToListview(); 

    } 
} 

而且還在listview的點擊我更換了新的片段,我也推測我的位置的元素。我有一個問題,只有「時間」 jsobobject 這是我的列表視圖的onclick Java代碼

main_listview.setOnItemClickListener(new OnItemClickListener() { 

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

      MoviewListResult newFragment = new MoviewListResult(); 

      FragmentTransaction transaction = getFragmentManager() 
        .beginTransaction(); 

      Bundle bundle = new Bundle(); 

      bundle.putString("image", arrayOfList.get(position) 
        .getBlurimage()); 
      bundle.putString("title", arrayOfList.get(position).getTitle()); 
      bundle.putString("trailer", arrayOfList.get(position) 
        .getYoutube()); 
      bundle.putString("category", arrayOfList.get(position) 
        .getCategory()); 
      bundle.putString("writer", arrayOfList.get(position) 
        .getWritten()); 
      bundle.putString("stars", arrayOfList.get(position).getStars()); 
      bundle.putString("geotime", arrayOfList.get(position) 
        .getGeorgia_time()); 
      bundle.putString("worldtime", arrayOfList.get(position) 
        .getWold_time()); 
      bundle.putString("descraption", arrayOfList.get(position) 
        .getDescraption()); 

      bundle.putString("time", arrayOfList.get(position).getTime()); 

      newFragment.setArguments(bundle); 
      transaction.replace(R.id.content_frame, newFragment); 

      transaction.commit(); 

     } 
    }); 

我到底做錯了什麼?如果有人知道的解決方案,請幫助我 詩 我是新用戶,我不能發佈圖片和請看我Url.i上傳我的形象

回答

0

用這個作爲JSONParser:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.impl.client.DefaultHttpClient; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.util.Log; 
public class JSONParser { 
    static InputStream is = null; 
    static JSONObject jObj = null; 
    static String json = ""; 
    // constructor 
    public JSONParser() { 
    } 
    public JSONObject getJSONFromUrl(String url) { 
    // Making HTTP request 
    try { 
     // defaultHttpClient 
     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     is = httpEntity.getContent(); 
    } catch (UnsupportedEncodingException e) { 
     e.printStackTrace(); 
    } catch (ClientProtocolException e) { 
     e.printStackTrace(); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
    try { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(
      is, "iso-8859-1"), 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) { 
     Log.e("Buffer Error", "Error converting result " + e.toString()); 
    } 
    // try parse the string to a JSON object 
    try { 
     jObj = new JSONObject(json); 
    } catch (JSONException e) { 
     Log.e("JSON Parser", "Error parsing data " + e.toString()); 
    } 
    // return JSON String 
    return jObj; 
    } 
} 

然後這裏是活性的AsyncTask實現:

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 
import android.app.Activity; 
import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 
import learn2crack.asynctask.library.JSONParser; 
public class MainActivity extends Activity { 
    TextView uid; 
    TextView name1; 
    TextView email1; 
    Button Btngetdata; 
    //URL to get JSON Array 
    private static String url = "http://10.0.2.2/JSON/"; 
    //JSON Node Names 
    private static final String TAG_USER = "user"; 
    private static final String TAG_ID = "id"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_EMAIL = "email"; 
    JSONArray user = null; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Btngetdata = (Button)findViewById(R.id.getdata); 
     Btngetdata.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      new JSONParse().execute(); 
     } 
    }); 
    } 
    private class JSONParse extends AsyncTask<String, String, JSONObject> { 
     private ProgressDialog pDialog; 
     @Override 
     protected void onPreExecute() { 
      super.onPreExecute(); 
      uid = (TextView)findViewById(R.id.uid); 
     name1 = (TextView)findViewById(R.id.name); 
     email1 = (TextView)findViewById(R.id.email); 
      pDialog = new ProgressDialog(MainActivity.this); 
      pDialog.setMessage("Getting Data ..."); 
      pDialog.setIndeterminate(false); 
      pDialog.setCancelable(true); 
      pDialog.show(); 
     } 
     @Override 
     protected JSONObject doInBackground(String... args) { 
     JSONParser jParser = new JSONParser(); 
     // Getting JSON from URL 
     JSONObject json = jParser.getJSONFromUrl(url); 
     return json; 
     } 
     @Override 
     protected void onPostExecute(JSONObject json) { 
     pDialog.dismiss(); 
     try { 
      // Getting JSON Array 
      user = json.getJSONArray(TAG_USER); 
      JSONObject c = user.getJSONObject(0); 
      // Storing JSON item in a Variable 
      String id = c.getString(TAG_ID); 
      String name = c.getString(TAG_NAME); 
      String email = c.getString(TAG_EMAIL); 
      //Set JSON Data in TextView 
      uid.setText(id); 
      name1.setText(name); 
      email1.setText(email); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     } 
    } 
} 

的詳細信息:

http://www.learn2crack.com/2013/10/android-asynctask-json-parsing-example.html

+0

謝謝,但我有JsonParse類(Utils).meybe你不瞭解我。我解析了json,但我只有一個問題信息的數組的時間object.please看到我的形象 – lukaaa 2014-10-02 21:00:51

相關問題