试图在android listview中获取mysql数据

时间:2016-03-07 12:16:28

标签: android mysql json

大家好我试图从android ListView中的mysql数据库中获取数据,

以下代码正在抛出java.lang.StringIndexOutOfBoundsException

我是Android开发的新手,如果有人能指导我完成这项工作,那将会有很大的帮助。 谢谢。

        import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.io.InputStream;
    import java.util.ArrayList;
    import android.app.Activity;
    import android.app.ProgressDialog;
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ListView;
    import org.apache.http.HttpEntity;
    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.json.JSONArray;
    import org.json.JSONObject;
    import android.os.AsyncTask;

public class Main_catagory_one extends Activity{
    Activity context;
            HttpPost httppost;
            StringBuffer buffer;
            HttpResponse response;
            HttpClient httpclient;
            ProgressDialog pd;
            CustomAdapter adapter;
            ListView listProduct;
            ArrayList<Product> records;

            protected void onCreate(Bundle savedInstanceState) {

            //TODO Auto-generated method stub

            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main1);

            context=this;

            records=new ArrayList<Product>();

            listProduct=(ListView)findViewById(R.id.listView1);

            adapter=new CustomAdapter(context, R.layout.list_view,R.id.textView3, 
            records);

            listProduct.setAdapter(adapter);

            }

            public void onStart(){

            super.onStart();

            //execute background task

            BackTask bt=new BackTask();

            bt.execute();

            }

            //background process to make a request to server and list product information

            private class BackTask extends AsyncTask<Void,Void,Void>{

            protected void onPreExecute(){

            super.onPreExecute();

            pd = new ProgressDialog(context);

            pd.setTitle("Retrieving data");

            pd.setMessage("Please wait.");

            pd.setCancelable(true);

            pd.setIndeterminate(true);

            pd.show();

            }

            protected Void doInBackground(Void...params){

            InputStream is=null;

            String result="";

            try{

                    httpclient=new DefaultHttpClient();

                    httppost= new HttpPost("http://10.0.2.2/fetch_item/getproducts.php");

                    response=httpclient.execute(httppost);

                    HttpEntity entity = response.getEntity();

                    // Get our response as a String.

                    is = entity.getContent();



                }catch(Exception e){

                        if(pd!=null)

                        pd.dismiss(); //close the dialog if error occurs

                        Log.e("ERROR", e.getMessage());

                }



            //convert response to string

            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();

            result=sb.toString();

            }catch(Exception e){

            Log.e("ERROR", "Error converting result "+e.toString());



            }



            //parse json data

            try{

            // Remove unexpected characters that might be added to beginning of the string

            result=result.substring(result.indexOf("["));

            JSONArray jArray =new JSONArray(result);

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

            JSONObject json_data =jArray.getJSONObject(i);

            Product p=new Product();

            p.setpName(json_data.getString("Item_name"));

            p.setuPrice(json_data.getString("Item_price"));

            p.setpQauntity(json_data.getString("Item_quantity"));

            p.setpImage(json_data.getString("Item_img"));

            records.add(p);

            }

            }

            catch(Exception e){

            Log.e("ERROR", "Error pasting data "+e.toString());
            }

            return null;

            }

            protected void onPostExecute(Void result){

            if(pd!=null) pd.dismiss(); //close dialog

            Log.e("size", records.size() + "");

            adapter.notifyDataSetChanged(); //notify the ListView to get new records

            }

            }

            }

1 个答案:

答案 0 :(得分:0)

  

IndexOutOfBoundsException - 如果beginIndex为负数或大于   这个String对象的长度。

您应该习惯使用API​​。它告诉您方法抛出的异常以及原因。 尝试在执行子字符串之前和之后打印字符串的长度。 例如......

String testing = "Hello World";
System.out.println("Length of testing = " + testing.length);
System.out.println("Value of testing = " + testing);
testing.substring(1,2);