Php Android mysql连接

Php Android mysql连接,php,android,mysql,Php,Android,Mysql,我正在为android开发一个应用程序,它通过php脚本连接到外部MySQL数据库以获取数据。问题是数据量太大,在尝试加载数据时遇到outofmemoryexception。我使用分页方法一次显示10个项目——当用户滚动到listview的底部时,将显示下10个项目。这里的问题是加载速度非常慢。我需要使应用程序响应。我应该使用什么方法?我使用的代码如下- public class ItemActivity extends Activity { private String str =

我正在为android开发一个应用程序,它通过php脚本连接到外部MySQL数据库以获取数据。问题是数据量太大,在尝试加载数据时遇到outofmemoryexception。我使用分页方法一次显示10个项目——当用户滚动到listview的底部时,将显示下10个项目。这里的问题是加载速度非常慢。我需要使应用程序响应。我应该使用什么方法?我使用的代码如下-

public class ItemActivity extends Activity {

    private String str = null;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
        .detectDiskReads().detectDiskWrites().detectNetwork() // StrictMode
        // is
        // most
        // commonly
        // used
        // to
        // catch
        // accidental
        // disk
        // or
        // network
        // access
        // on
        // the
        // application's
        // main
        // thread
        .penaltyLog().build());
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_item);
        setTitle("Kurd Shopping");

        final int id=getIntent().getExtras().getInt("id");
        try{
            ArrayList<NameValuePair> postParameters=new ArrayList<NameValuePair>();
            postParameters.add(new BasicNameValuePair("id",String.valueOf(id)));
            postParameters.add(new BasicNameValuePair("startid", "0"));
            str=CustomHttpClient.executeHttpPost("http://kurdshopping.net/apj/adlist.php", postParameters);
            JSONArray array=new JSONArray(str);
            final Item[] item_data=new Item[array.length()];

            for(int i=0;i<item_data.length;i++){
                JSONObject jdata=array.getJSONObject(i);
                String path="http://kurdshopping.net/thumbs/"+jdata.getString("name");
                item_data[i]=new Item(jdata.getInt("id"), path, jdata.getString("title")+"\nPrice: "+jdata.getString("price"));
            }
            final ItemAdapter adapter=new ItemAdapter(this, R.layout.listview_item_row, item_data);
            final ListView listView=(ListView) findViewById(R.id.listView1);
            listView.setAdapter(adapter);

            listView.setOnItemClickListener(new OnItemClickListener() {

                @Override
                public void onItemClick(AdapterView<?> arg0, View arg1,
                        int position, long arg3) {
                    Toast.makeText(ItemActivity.this, item_data[position].id, Toast.LENGTH_SHORT).show();

                }
            });
            listView.setOnScrollListener(new OnScrollListener() {

                @Override
                public void onScrollStateChanged(AbsListView view, int scrollState) {
                    // TODO Auto-generated method stub

                }

                @Override
                public void onScroll(AbsListView view, int firstVisibleItem,
                        int visibleItemCount, int totalItemCount) {
                    int lastVisibleitem=firstVisibleItem+visibleItemCount;
                    if(lastVisibleitem==totalItemCount){
                        ArrayList<NameValuePair> postParameters=new ArrayList<NameValuePair>();
                        postParameters.add(new BasicNameValuePair("id",String.valueOf(id)));
                        String s=String.valueOf(item_data[lastVisibleitem-1].id);
                        postParameters.add(new BasicNameValuePair("startid", s));
                        try {
                            str=CustomHttpClient.executeHttpPost("http://kurdshopping.net/apj/adlist.php", postParameters);
                            JSONArray array=new JSONArray(str);
                            for(int i=0;i<item_data.length;i++){
                                JSONObject jdata=array.getJSONObject(i);
                                String path="http://kurdshopping.net/thumbs/"+jdata.getString("name");
                                item_data[i]=new Item(jdata.getInt("id"), path, jdata.getString("title")+"\nPrice: "+jdata.getString("price"));
                            }
                            adapter.notifyDataSetChanged();
                            listView.setSelection(0);
                        }catch(JSONException j){
                            Log.e("ItemActivity", j.getMessage());
                        } catch (Exception e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }

                    }else if(lastVisibleitem==totalItemCount){

                    }
                }
            });
        }catch(NotFoundException n){
            Log.e("ItemActivity", n.getMessage());
        }catch(JSONException j){
            Log.e("ItemActivity", j.getMessage());
        }catch (Exception e) {
            Log.e("ItemActivity", e.getMessage());
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.item, menu);
        return true;
    }

}
公共类ItemActivity扩展了活动{
私有字符串str=null;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
StrictMode.setThreadPolicy(新的StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()//StrictMode
//是
//大多数
//通常
//使用
//到
//抓住
//偶然的
//圆盘
//或
//网络
//通路
//在
//
//申请书
//主要
//线
.penaltyLog().build());
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_项);
setTitle(“库尔德购物”);
final int id=getIntent().getExtras().getInt(“id”);
试一试{
ArrayList后参数=新的ArrayList();
添加(新的BasicNameValuePair(“id”,String.valueOf(id));
添加(新的BasicNameValuePair(“startid”,“0”));
str=CustomHttpClient.executeHttpPost(“http://kurdshopping.net/apj/adlist.php“,后参数);
JSONArray数组=新的JSONArray(str);
最终项[]项_数据=新项[array.length()];

对于(int i=0;我能提供代码片段来说明这一点吗?谷歌曾经在android中延迟加载