Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8966379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T17:00:57+00:00 2026-06-15T17:00:57+00:00

my app will load data from server and save it to database sqlite on

  • 0

my app will load data from server and save it to database sqlite on device android.My problem is when i want test duplication of data in the database -> my app error. Can you help me!error on postion “*” at code
CLASS DB_Adapter query on database

public class DB_Adapter {
public static final String INVOICEID = "invoiceid";
public static final String NUMBERPHONE = "numberphone";
public static final String ADDRESS = "address";
public static final String NUMBER = "number";
public static final String SNAME = "name";
public static final String URL = "url";
public static final String STATUS = "status";

public static final String DB_NAME = "CSDL";
public static final String DB_TABLE = "Saigonfleamarket";
public static final int DB_VERSION = 2;

private final Context mContext;
private SQLiteDatabase mDB;
private DBHelper mDBHelper;

public DB_Adapter(Context c) {
    mContext = c;
}

private static class DBHelper extends SQLiteOpenHelper
{

    public DBHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try
        {
            db.execSQL("CREATE TABLE Saigonfleamarket (number integer PRIMARY KEY autoincrement, invoiceid text,name text, numberphone text, address text,url text, status text);");
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        Log.i("DBAdapter", "Updating database...");
        db.execSQL("DROP TABLE IF EXISTS Saigonfleamarket");
        onCreate(db);
    }
}

public DB_Adapter openDB()
{
    mDBHelper = new DBHelper(mContext, DB_NAME, null, DB_VERSION);
    mDB = mDBHelper.getWritableDatabase();
    return this;
}

public void closeDB()
{
    mDBHelper.close();
}

public long insert(String _invoiceid, String _name, String _numberphone, String _address,
        String _url, String _status)
{
    ContentValues cv = new ContentValues();
    cv.put(INVOICEID, _invoiceid);
    cv.put(SNAME, _name);
    cv.put(NUMBERPHONE, _numberphone);
    cv.put(ADDRESS, _address);
    cv.put(URL, _url);
    cv.put(STATUS, _status);
    return mDB.insert(DB_TABLE, null, cv);
}

public boolean remove(int _number)
{
    return mDB.delete(DB_TABLE, INVOICEID + "=" + _number, null) > 0;
}

public Cursor getAllStudent()
{
    return mDB.query(DB_TABLE, new String[] {
            NUMBER, INVOICEID, SNAME, NUMBERPHONE, ADDRESS, URL, STATUS
    }, null, null, null, null, null);
}

public Cursor getStudentById(int _number)
{
    Cursor mCursor = mDB.query(true, DB_TABLE, new String[] {
            NUMBER, INVOICEID, SNAME, NUMBERPHONE, ADDRESS, URL, STATUS
    }, NUMBER + "=" + _number, null, null, null, null, null);
    if (mCursor != null)
    {
        mCursor.moveToFirst();
    }

    return mCursor;
}

***public Boolean test (String mahoadon)***
{
    Boolean flag=true;
    Cursor mCursor = mDB.query(true, DB_TABLE, new String[] {
            NUMBER, INVOICEID, SNAME, NUMBERPHONE, ADDRESS, URL, STATUS
    }, INVOICEID + "=" + mahoadon, null, null, null, null, null);
    if (mCursor != null)
    {
        flag=false;
    }
    else
    {
        flag=true;
    }

    return flag;
}

}

CLASS LOAD DATA AND SAVE TO DATABASE

private DB_Adapter mDb = new DB_Adapter(this); 
// class load data từ server về thiết bị
private class LoadData extends AsyncTask<Void, Void, Void> {
    private ProgressDialog progressDialog;
    private Boolean flag = false;
    String phonenumber;
    String name;
    String address;
    String url;
    String code;
    private JSONArray jArray;
    private String result = null;
    private InputStream is = null;
    private StringBuilder sb = null;
    private ArrayList<String> phonenumberArray = new ArrayList<String>();
    private ArrayList<String> codeInvoiceArray = new ArrayList<String>();
    private ArrayList<String> nameArray = new ArrayList<String>();
    private ArrayList<String> addressArray = new ArrayList<String>();
    private ArrayList<String> urlArray = new ArrayList<String>();
    private ListView listview;


    @Override
    // can use UI thread here
    protected void onPreExecute() {
        this.progressDialog = ProgressDialog.show(Hoadon.this, "",
                " Đang tải thông tin...");
    }

    // hiển thị thông tin lên listview của Activity
    @Override
    protected void onPostExecute(final Void unused) {
        this.progressDialog.dismiss();
        if (flag == true)
        {

            thongbao("Tải dữ liệu thành công");
        }
        else
        {
            thongbao("Hôm nay không có gì để giao!");
        }
        try {
            listview = (ListView) findViewById(R.id.listView1);
            this.progressDialog.dismiss();
            listview.setAdapter(new DataAdapter(Hoadon.this, codeInvoiceArray
                    .toArray(new String[codeInvoiceArray.size()]), nameArray
                    .toArray(new String[nameArray.size()]),phonenumberArray
                    .toArray(new String[phonenumberArray.size()]), addressArray
                    .toArray(new String[addressArray.size()])));
            listview.setOnItemClickListener(new OnItemClickListener() {

                //xử lý sự kiện khi chọn item trên listview
                @Override
                public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    Intent j=new Intent(Hoadon.this,Menu_Main.class);

                    //lấy thông tin thông qua vị trí của item được chọn
                    String url = urlArray.get(position);
                    String address = addressArray.get(position);
                    String mahoadon = codeInvoiceArray.get(position);
                    String phone=phonenumberArray.get(position);

                    //chuyển thông tin này qua bên phần ký tên
                    j.putExtra("url", url);
                    j.putExtra("address", address);
                    j.putExtra("mahoadon", mahoadon);
                    j.putExtra("phone",phone);
                    j.putExtra("ID", IDinvoice);
                    startActivity(j);
                }
            });
        } catch (Exception e) {

            Toast.makeText(getApplicationContext(), e.toString(),
                    Toast.LENGTH_LONG).show();
        }
    }

    // lấy thông tin từ server về dưới dạng json, được chuyển thành chuỗi và
    // lưu vào các mảng
    @Override
    protected Void doInBackground(Void... params) {
        try {
            /*Calendar cal = Calendar.getInstance();
            int date1 = cal.get(Calendar.DAY_OF_MONTH);
            int month = cal.get(Calendar.MONTH) + 1;
            int year = cal.get(Calendar.YEAR);
            String str1 = date1 + "/" + month + "/" + year;*/

            //Gửi thông tin id người giao hàng lên server để lấy danh sách hóa đơn về
            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(
                    "http://longvansolution.tk/monthlytarget.php");
            nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("user", IDinvoice));
            /*nameValuePairs.add(new BasicNameValuePair("mydate", str1));*/
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            is = entity.getContent();

            //chuyển thông tin nhận được về dạng chuỗi
            BufferedReader reader = new BufferedReader(
                    new InputStreamReader(is, "iso-8859-1"), 80);
            sb = new StringBuilder();
            sb.append(reader.readLine() + "\n");
            String line = "0";
            while ((line = reader.readLine()) != null) {
                sb.append(line + "\n");
            }
            is.close();
            result = sb.toString();
        } catch (Exception e) {
            Toast.makeText(getApplicationContext(), e.toString(),
                    Toast.LENGTH_LONG).show();
        }

        //kiểm tra thông tin nhận được từ server 
        if (result.equalsIgnoreCase("null\n\n \n"))
        {
            flag = false;

            //làm sạch mảng trước khi muốn thêm các thành phần để tránh tình trạng trùng lặp
            phonenumberArray.clear();
            codeInvoiceArray.clear();
            nameArray.clear();
            addressArray.clear();
            urlArray.clear();
        } else
        {
            flag = true;
            try {
                jArray = new JSONArray(result);
                JSONObject json_data = null;
                phonenumberArray.clear();
                codeInvoiceArray.clear();
                nameArray.clear();
                addressArray.clear();
                urlArray.clear();
                mDb.openDB();

                //sử dụng json lấy các giá trị thông qua các key
                //gán nó vào các mảng tương ứng
                for (int i = 0; i < jArray.length(); i++) {
                    json_data = jArray.getJSONObject(i);
                    phonenumber = json_data.getString("phonenumber");
                    address = json_data.getString("address");
                    name = json_data.getString("name");
                    url = json_data.getString("url");
                    code = json_data.getString("code");
                    phonenumberArray.add(phonenumber);
                    codeInvoiceArray.add(code);
                    nameArray.add(name);
                    addressArray.add(address);
                    urlArray.add(url);
                    ***if(mDb.test(code)==true)
                    {
                        mDb.insert(code, name, phonenumber, address, url, "Chuagiao");
                    }***
                }
            } catch (JSONException e) {
                Toast.makeText(getApplicationContext(), e.toString(),
                        Toast.LENGTH_LONG).show();

            } catch (ParseException e) {
                Toast.makeText(getApplicationContext(), e.toString(),
                        Toast.LENGTH_LONG).show();
            } catch (Exception e) {
                Toast.makeText(getApplicationContext(), e.toString(),
                        Toast.LENGTH_LONG).show();
            }
        }
        mDb.closeDB();
        return null;
    }
}

LOG ERROR

12-12 11:08:31.979: W/dalvikvm(1384): threadid=14: thread exiting with uncaught    exception (group=0x40015560)
12-12 11:08:31.987: E/AndroidRuntime(1384): FATAL EXCEPTION: AsyncTask #4
12-12 11:08:31.987: E/AndroidRuntime(1384): java.lang.RuntimeException: An error occured while executing doInBackground()
12-12 11:08:31.987: E/AndroidRuntime(1384):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at java.lang.Thread.run(Thread.java:1019)
12-12 11:08:31.987: E/AndroidRuntime(1384): Caused by: java.lang.RuntimeException: Can't  create handler inside thread that has not called Looper.prepare()
12-12 11:08:31.987: E/AndroidRuntime(1384):     at android.os.Handler.<init>(Handler.java:121)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at android.widget.Toast.<init>(Toast.java:68)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at android.widget.Toast.makeText(Toast.java:231)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at com.example.finishdemo.Hoadon$LoadData.doInBackground(Hoadon.java:437)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at com.example.finishdemo.Hoadon$LoadData.doInBackground(Hoadon.java:1)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
12-12 11:08:31.987: E/AndroidRuntime(1384):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
12-12 11:08:31.987: E/AndroidRuntime(1384):     ... 4 more
12-12 11:08:33.347: E/WindowManager(1384): Activity com.example.finishdemo.Hoadon has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405aa248 that was originally added here
12-12 11:08:33.347: E/WindowManager(1384): android.view.WindowLeaked: Activity com.example.finishdemo.Hoadon has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@405aa248 that was originally added here
12-12 11:08:33.347: E/WindowManager(1384):  at android.view.ViewRoot.<init>(ViewRoot.java:258)
12-12 11:08:33.347: E/WindowManager(1384):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
12-12 11:08:33.347: E/WindowManager(1384):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
12-12 11:08:33.347: E/WindowManager(1384):  at android.view.Window$LocalWindowManager.addView(Window.java:424)
12-12 11:08:33.347: E/WindowManager(1384):  at android.app.Dialog.show(Dialog.java:241)
12-12 11:08:33.347: E/WindowManager(1384):  at   android.app.ProgressDialog.show(ProgressDialog.java:107)
12-12 11:08:33.347: E/WindowManager(1384):  at android.app.ProgressDialog.show(ProgressDialog.java:90)
12-12 11:08:33.347: E/WindowManager(1384):  at android.app.ProgressDialog.show(ProgressDialog.java:85)
12-12 11:08:33.347: E/WindowManager(1384):  at com.example.finishdemo.Hoadon$LoadData.onPreExecute(Hoadon.java:291)
12-12 11:08:33.347: E/WindowManager(1384):  at android.os.AsyncTask.execute(AsyncTask.java:391)
12-12 11:08:33.347: E/WindowManager(1384):  at com.example.finishdemo.Hoadon$4$1.run(Hoadon.java:246)
12-12 11:08:33.347: E/WindowManager(1384):  at android.os.Handler.handleCallback(Handler.java:587)
12-12 11:08:33.347: E/WindowManager(1384):  at android.os.Handler.dispatchMessage(Handler.java:92)
12-12 11:08:33.347: E/WindowManager(1384):  at android.os.Looper.loop(Looper.java:130)
12-12 11:08:33.347: E/WindowManager(1384):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-12 11:08:33.347: E/WindowManager(1384):  at java.lang.reflect.Method.invokeNative(Native Method)
12-12 11:08:33.347: E/WindowManager(1384):  at java.lang.reflect.Method.invoke(Method.java:507)
12-12 11:08:33.347: E/WindowManager(1384):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-12 11:08:33.347: E/WindowManager(1384):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-12 11:08:33.347: E/WindowManager(1384):  at dalvik.system.NativeStart.main(Native Method)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T17:00:57+00:00Added an answer on June 15, 2026 at 5:00 pm

    You cannot reference any UI elements, in this case Toasts, in doInBackground(). You need to save the error message, perhaps in a String, then display it in onPostExecute().

    Taking a closer look at your Toast, you are trying to display a very long message in a small Toast. Typically this information is keep from the user, since it of no use to them. At the most basic level, designers use:

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

    Addition
    The above technique will stop your app from crashing, but as you pointed out SQLite is still throwing an exception. I notice that you are not using a String correctly:

    Cursor mCursor = mDB.query(true, DB_TABLE, 
        new String[] {NUMBER, INVOICEID, SNAME, NUMBERPHONE, ADDRESS, URL, STATUS}, 
        INVOICEID + "=" + mahoadon, null, null, null, null, null);
    

    The correct syntax is:

    INVOICEID + "='" + mahoadon + "'"
    

    Though it is best to use the selectionArgs parameter:

    Cursor mCursor = mDB.query(true, DB_TABLE, 
        new String[] {NUMBER, INVOICEID, SNAME, NUMBERPHONE, ADDRESS, URL, STATUS}, 
        INVOICEID + "=?", new String[] {mahoadon}, null, null, null, null);
    

    This prevents SQL injections and possible caching problems.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

my app will load data from server and show on listview and save this
My app will auto load data when i run app.My problem is when my
Some articles on the web tell me that System.loadLibrary() will load files in /data/data/{app
In my Android app, the main Activity will sometimes start a Thread to load
I am trying to send data from an Android app to a PHP file
I want to load content into my application from a web server, but if
My app will allow users to create a personalised list of events from a
I am making an app like the Android market where all free app will
I have client/server application where the client app will open files. Those files get
I have a HighCharts client whose responsibility is to load data from a webservice

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.