【问题标题】:Probleme Threads Connection database mysql android问题线程连接数据库mysql android
【发布时间】:2013-07-15 01:43:21
【问题描述】:

我想知道线程或 AsyncTask 的实现者如何为我的连接和提取给定 android 2.3.3 上的 mysql 数据库它工作但不适用于 4.1 资源问题,谢谢我需要你的帮助

package com.example.test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.ArrayList;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;

import android.app.Activity;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Actu_Connextion extends Activity {

    Actualite oActualite;
    ArrayList<Actualite> listeActualites;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

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

        int actionBarTitleId = Resources.getSystem().getIdentifier("action_bar_title", "id", "android");
        TextView actionBarTextView = (TextView)findViewById(actionBarTitleId); 
        actionBarTextView.setTextColor(Color.WHITE);


        this.listeActualites = new ArrayList<Actualite>();

    }
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.refresh, menu);
        return true;
    }
     public  boolean onOptionsItemSelected(MenuItem item){        // Quand on appuis sur l/'item
            switch (item.getItemId()){

                case R.id.refresh:                       // selectionne l'item par id
                    return true;

                case R.id.back_menu_principale:                       // selectionne l'item par id
                    Intent intent = new Intent(getBaseContext() ,MainActivity.class);
                     startActivity(intent); 
                default:
                    return super.onOptionsItemSelected(item);
            }
        }

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

        StringBuffer stringB = new StringBuffer("");
        BufferedReader bufR = null;

        try{
            HttpClient client =  new DefaultHttpClient();
            HttpGet requete = new HttpGet();
            URI uri = new URI("http://192.168.1.5/actu.php");
            requete.setURI(uri);
            HttpResponse reponse = client.execute(requete);
            InputStream is = reponse.getEntity().getContent();
            bufR = new BufferedReader(new InputStreamReader(is));
            String ligneLue = bufR.readLine();
            while(ligneLue != null){
                stringB.append(ligneLue);
                stringB.append("\n");
                ligneLue = bufR.readLine();
            }
        }catch(Exception e){
                e.printStackTrace();
            }finally{
                if(bufR != null){
                    try{
                    bufR.close();
                    }catch(IOException ioe){
                        ioe.printStackTrace();
                        Toast.makeText(this, ioe.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }
        try{
            JSONArray jArray = new JSONArray(stringB.toString());
            for(int i = 0; i<jArray.length(); i++){
                oActualite = new Actualite();
                oActualite.setoContenu(jArray.getJSONObject(i).getString("contenu").toString());
                oActualite.setoChemin(jArray.getJSONObject(i).getString("chemin").toString());
                oActualite.setoDate(jArray.getJSONObject(i).getString("date").toString());

                this.listeActualites.add(oActualite);
            }
        }catch(JSONException jex){
            jex.printStackTrace();
            Toast.makeText(this, jex.getMessage(), Toast.LENGTH_LONG).show();
        }
        if(listeActualites != null){
            ActualiteAdapter adapter = new ActualiteAdapter(this, listeActualites);
            ListView listeVueActualites = (ListView) findViewById(R.id.lstActualites);
            listeVueActualites.setAdapter(adapter);
        }
    }
}

【问题讨论】:

    标签: android mysql android-asynctask


    【解决方案1】:

    将你的 http 连接放入 doInBackground 并从 postexecute 方法返回列表。更多信息请查看 [Android 开发者] 试试这个 (http://developer.android.com/reference/android/os/AsyncTask.html)

      private class DownloadTask extends AsyncTask<String, Integer, List<oActualite>>{
    
    
        // Invoked by execute() method of this object
        @Override
        protected List<oActualite> doInBackground(String... jsonData) {
    
           StringBuffer stringB = new StringBuffer("");
        BufferedReader bufR = null;
    
        try{
            HttpClient client =  new DefaultHttpClient();
            HttpGet requete = new HttpGet();
            URI uri = new URI("http://192.168.1.5/actu.php");
            requete.setURI(uri);
            HttpResponse reponse = client.execute(requete);
            InputStream is = reponse.getEntity().getContent();
            bufR = new BufferedReader(new InputStreamReader(is));
            String ligneLue = bufR.readLine();
            while(ligneLue != null){
                stringB.append(ligneLue);
                stringB.append("\n");
                ligneLue = bufR.readLine();
            }
        }catch(Exception e){
                e.printStackTrace();
            }finally{
                if(bufR != null){
                    try{
                    bufR.close();
                    }catch(IOException ioe){
                        ioe.printStackTrace();
                        Toast.makeText(this, ioe.getMessage(), Toast.LENGTH_LONG).show();
                    }
                }
            }
        try{
            JSONArray jArray = new JSONArray(stringB.toString());
            for(int i = 0; i<jArray.length(); i++){
                oActualite = new Actualite();
                oActualite.setoContenu(jArray.getJSONObject(i).getString("contenu").toString());
                oActualite.setoChemin(jArray.getJSONObject(i).getString("chemin").toString());
                oActualite.setoDate(jArray.getJSONObject(i).getString("date").toString());
    
                this.listeActualites.add(oActualite);
    
                 ActualiteAdapter adapter = new ActualiteAdapter(this, listeActualites);
             ListView listeVueActualites = (ListView) findViewById(R.id.lstActualites);
            }
        }catch(JSONException jex){
            jex.printStackTrace();
            Toast.makeText(this, jex.getMessage(), Toast.LENGTH_LONG).show();
        }
        }
    
        // Executed after the complete execution of doInBackground() method
        @Override
        protected void onPostExecute(List<oActualite> list){
            ListView listView = ( ListView ) findViewById(R.id.listview);
            listView.setAdapter(adapter);
            }
        }
    }
    

    }

    【讨论】:

    • 我还不太明白如何在我的情况下使用参数:/
    • 我在使用您的代码代替 Arraylist List 的适配器中有错误,我如何才能确定西装中的错误适合未定义的构造函数
    猜你喜欢
    • 1970-01-01
    • 2016-12-08
    • 2011-10-07
    • 2015-01-21
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    • 1970-01-01
    相关资源
    最近更新 更多