java.lang.StringIndexOutOfBoundsException:length = 75; regionStart = 0; regionLength = 100

时间:2014-11-19 18:34:59

标签: java android json

我在Logcat上收到警告,但仍然无法找到解决方法。也许有人可以告诉我解决方案。为什么logcat不能显示我的所有JSON?它只展示了其中一些。这是正常的还是我应该在我的代码中添加一些内容?

11-19 12:40:46.296: I/.........(1644): [{"id":"119","judul":"Daftar Penerima Beasiswa Bidik Misi","kategori":"beasiswa","published":"2014-09-17 06:27:19","isi":"Silhkan klik link di bawah ini : Penerima Beasiswa Bidik Misi Terima kasih."},{"id":"118","judul":"Penerima Beasiswa Toyota dan Astra","kategori":"beasiswa","published":"2014-09-17 06:26:48","isi":"Kepada mahasiswa yang namanya tercantum dalam\u00a0link berikut ini Mohon segera datang ke kantor bikem untuk realisasi beasiswa Toyota dan Astra Terima kasih."},{"id":"117","judul":"BAKTI SOSIAL TANAM POHON DI KAMPUS III","kategori":"beasiswa","published":"2014-09-17 06:26:18","isi":"Seluruh penerima beasiswa UKSW diundang hadir (wajib) dalam acara tanam pohon pada hari Sabtu, 16 November 2012 jam 06.30 di kampus III. Mahasiswa langsung menuju ke kampus III desa blotongan dengan membawa air untuk menyiram, alat menanam, penyangga bibit dari bambu (tinggi 1 M dan lebar 4 cm), rafia, label nama dilapis plastik (10 cm x 12 cm) (lubang sudah disediakan) daftar ketua kelompok dan nomor HP dapat dilihat di papan pengumuman, info beasiswa, dan web."},{"id":"116","judul":"Pendidikan Agama Kelas H (Dosen : Bpk. Thobias Messakh)","kategori":"lainnya","published":"2014-09-17 06:25:24","isi":"Jadwal Matakuliah Pendidikan Agama Kelas H yang diampu oleh Bpk. Dr. Thobias Messakh ialah Hari Kamis, 16-18 Ruang di ABX102. Mohon untuk diperhatikan. Terima Kasih"},{"id":"115","judul":"Program MAGANG di PT. KIBAR KREASI INDONESIA","kategori":"lainnya","published":"2014-09-17 06:24:57","isi":"Kalau kamu anak muda yang mau bikin perubahan, tertarik bikin proyek yang berguna buat orang banyak, dan mau mencari pengalaman yang bisa memaksimalkan potensi kamu, yuk gabung... PT. KIBAR KREASI INDONESIA menajak Mahasiswa UKSW untuk bergabung dalam program Magang selama 2 Bulan...Syarat & Ketentuan lebih lanjut bisa dilihat di link berikut :\u00a0http:\/\/goo.gl\/erZALk Terima Kasih. Salam, Google Student Ambassador"},{"id":"114","judul":"Panggilan Mahasiswa (DKV)","kategori":"lainnya","published":"2014-09-17 06:24:27","isi":"Panggilan kepada mahasiswa atas nama GLADYS HANNEKE LOUDY NIKIJULUW (692011055) mohon segera menghadap Bp. Michael di kantor pada hari Rabu, 17 September 2014 jam 12.00 WIB. Harap diperhatikan. \u00a0 Terima kasih, \u00a0 Salam, Michael"},{"id":"113","judul":"Panggilan Mahasiswa TI","kategori":"lowongan-pekerjaan","published":"2014-09-17 06:23:42","isi":"Mohon mahasiswa dengan NIM dibawah ini datang ke kantor kaprogdi S1 TI paling lambat hari Rabu, 4 Juni 2014. Berikut daftar NIM nya : - 67 2007 020 (Catur Heru Nugroho) - 67 2007 026 (Arya Berliandi) - 67 2007 156 (Ayu Wulan Sari) - 67 2007 183 (Andreas Vipri Nandha Gunadi S) - 67 2007 289 (Febriyanto Heri S) - 67 2009 003 (Georgius Teniwut) - 67 2010 612 - 67 2011 705 - 67 2011 712 - 67 2012 707 \u00a0 Demikian atas perhatiannya terima kasih \u00a0 Salam, Kaprogdi S1 TI"},{"id":"112","judul":"Undangan Pendidikan Publik (Jurnal Perempuan)","kategori":"lowongan-pekerjaan","published":"2014-09-17 06:23:06","isi":"Untuk info lebih lengkap silahkan klik\u00a0disini."},{"id":"111","judul":"Lowongan Kerja Formulatrix","kategori":"lowongan-pekerjaan","published":"2014-09-17 06:22:33","isi":"Berikut ini lowongan yang sedang dibuka saat ini di Formulatrix, silahkan klik link dibawah ini:  RnD Mechanical Design Engineer RnD Mechanical Design Engineer (Internship), special qualification: minimum 6 month - 1 year in the final year of study RnD Junior Electronic Engineer --\u00a0http:\/\/siva-id.jobstreet.com\/_ads\/id\/jobs\/2014\/7\/new\/p\/40\/1319232.htm?17145620 RnD Junior Electronic Engineer (Internship ), special qualification: Minimum 6 month - 1 year in the final year of study Firmware Engineer --\u00a0http:\/\/siva-id.jobstreet.com\/_ads\/id\/jobs\/2014\/7\/new\/p\/40\/1319217.htm?17145342 RnD Senior Software Engineer --\u00a0http:\/\/siva-id.jobstreet.com\/_ads\/id\/jobs\/2014\/7\/new\/p\/40\/1319207.htm?17144918 RnD Junior Software Engineer (Internship), special qualification: Minimum 6 month - 1 year in the final year of study HR
11-19 12:40:47.086: W/System.err(1644): java.lang.StringIndexOutOfBoundsException: length=75; regionStart=0; regionLength=100
11-19 12:40:47.086: W/System.err(1644):     at java.lang.String.startEndAndLength(String.java:588)
11-19 12:40:47.106: W/System.err(1644):     at java.lang.String.substring(String.java:1475)
11-19 12:40:47.106: W/System.err(1644):     at com.example.cobalagi.AksesServerActivity$LoadAllPengumuman.doInBackground(AksesServerActivity.java:1)
11-19 12:40:47.106: W/System.err(1644):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
11-19 12:40:47.106: W/System.err(1644):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
11-19 12:40:47.106: W/System.err(1644):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
11-19 12:40:47.106: W/System.err(1644):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
11-19 12:40:47.106: W/System.err(1644):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
11-19 12:40:47.106: W/System.err(1644):     at java.lang.Thread.run(Thread.java:841)
11-19 12:40:47.106: W/System.err(1644):     at com.example.cobalagi.AksesServerActivity$LoadAllPengumuman.doInBackground(AksesServerActivity.java:111)
11-19 12:40:47.266: I/ActivityManager(1273): Displayed com.example.cobalagi/.AksesServerActivity: +1s932ms
11-19 12:40:47.266: I/Choreographer(1273): Skipped 48 frames!  The application may be doing too much work on its main thread.
11-19 12:40:47.306: I/WindowManager(1273): Switching to real app window: Window{b32ad1f0 u0 com.example.cobalagi/com.example.cobalagi.AksesServerActivity}
11-19 12:40:53.626: D/dalvikvm(1380): GC_FOR_ALLOC freed 533K, 16% free 3413K/4048K, paused 10ms, total 20ms

这是我的班级:

public class AksesServerActivity extends ListActivity {

    private ProgressDialog pDialog;

    //JSONParser jParser = new JSONParser();

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

    private static String link_url = "http://10.0.2.2/Pengumuman/get_all_pengumuman.php";

    private static final String PNG_ID = "id";
    private static final String PNG_JUDUL = "judul";
    private static final String PNG_KATEGORI = "kategori";
    private static final String PNG_PUBLISHED = "published";
    private static final String PNG_ISI = "isi";

    JSONArray pengumuman = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        daftar_pengumuman = new ArrayList<HashMap<String, String>>();
        new LoadAllPengumuman().execute();
        ListView lv = getListView();
        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String kode = ( (TextView) view.findViewById(R.id.kode) ).getText().toString();

                Intent in = new Intent(getApplicationContext(), DetailAksesServer.class);
                in.putExtra(PNG_ID, kode);
                startActivity(in);
            }
        });
    }

    class LoadAllPengumuman extends AsyncTask<String, String, String> {
    @Override
        protected void onPreExecute() {
            super.onPreExecute();
            pDialog = new ProgressDialog(AksesServerActivity.this);
            pDialog.setMessage("Loading Pengumuman. Harap Tunggu . . .");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(false);
            pDialog.show();
        }

        protected String doInBackground(String... args) {
            try {
                Log.i(".........", "Hellooooo......");
                DefaultHttpClient httpClient = new DefaultHttpClient();
                HttpGet httpPost = new HttpGet(link_url);
                HttpResponse response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                String jsonString = EntityUtils.toString(entity);
                Log.i(".........", jsonString);
                //JSONObject json = new JSONObject(jsonString);
                //JSONArray jArray = json.getJSONArray(PNG_ID);

                //JSONObject json = jParser.ambilJson(link_url);

                //Log.d("Pengumuman:", json.toString());


                JSONArray jArray = new JSONArray(jsonString);

                for(int i = 0; i < jArray.length(); i++) {
                    JSONObject a = jArray.getJSONObject(i);

                    //String id = a.getString(PNG_ID);
                    String judul = a.getString(PNG_JUDUL);
                    String kategori = a.getString(PNG_KATEGORI);
                    String published = a.getString(PNG_PUBLISHED);
                    String isi = a.getString(PNG_ISI).substring(0, 100)+"...(baca selengkapnya)";                       
                    HashMap<String, String> map = new HashMap<String, String>();

                    //map.put(PNG_ID, id);
                    map.put(PNG_JUDUL, judul);
                    map.put(PNG_KATEGORI, kategori);
                    map.put(PNG_PUBLISHED, published);
                    map.put(PNG_ISI, isi);

                    daftar_pengumuman.add(map);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            return null;
        }

        protected void onPostExecute(String file_url) {
            pDialog.dismiss();

            runOnUiThread(new Runnable() {
                public void run() {
                    //public void adapter_listview() {
                    ListAdapter adapter = new SimpleAdapter(AksesServerActivity.this, daftar_pengumuman,
                    R.layout.list_item,
                    new String[] { PNG_JUDUL, PNG_KATEGORI, PNG_PUBLISHED, PNG_ISI, PNG_ID}, 
                        new int[] {R.id.judul, R.id.kategori, R.id.published, R.id.isi, R.id.kode });
                    setListAdapter(adapter);

                }
            });
        }
    }

    public static void longLog(String str) {
        if (str.length() > 4000) {
            Log.d("", str.substring(0, 4000));
            longLog(str.substring(4000));
        } else
            Log.d("", str);
    }
}

感谢所有帮助

2 个答案:

答案 0 :(得分:1)

在进行一个假设某个字符串长度的调用之前:

String isi = a.getString(PNG_ISI).substring(0, 100)+"...(baca selengkapnya)";

你必须确保你的String至少有100个字符:

String isi = "";
String png = a.getString(PNG_ISI);
if (png.length() >= 100)
    isi = png.substring(0, 100)+"...(baca selengkapnya)";
else 
    // do something else

答案 1 :(得分:0)

您的PNG_ISI字符串长度不是100个字符,即使您要求系统返回长度为100个字符的子字符串。你可以通过两种方式处理这个问题 - 第一种是跳过&#34;读更多&#34;文本如果不是100个字符长,另一个是只添加一个&#34;阅读更多&#34;你不管怎么做的文字。

// if you only want a "read more" suffix for long strings
String isi = a.getString(PNG_ISI);
if (isi.length() >= 100) {
    isi = isi.substring(0, 100) + "...(baca selengkapnya)";
}

// if you want to always have a "read more" suffix
String isi = a.getString(PNG_ISI);
isi = isi.substring(0, Math.min(isi.length(), 100)) + "...(baca selengkapnya)";