我正在尝试将来自Web服务的信息存储到sqlite数据库中。但是,当我尝试存储数据时,我收到一个致命错误:MAIN错误和空指针异常。这是我的代码。任何帮助都会非常有帮助。 thx提前。
MainActivity:
public class MainActivity extends Activity {
SQLiteDB sqlite_obj;
Button get, store;
List<String> list1, list2;
InputStream is = null;
String ip = "http://XXXXX.com/select.php?jszam=0000";
String line = null;
String result = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sqlite_obj = new SQLiteDB(MainActivity.this);
get = (Button) findViewById(R.id.button1);
store = (Button) findViewById(R.id.button2);
get.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
webservice();
Toast.makeText(getBaseContext(), "Success : Webservice Call", Toast.LENGTH_SHORT).show();
}
});
store.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
sqlite();
Toast.makeText(getBaseContext(), "Stored in SQLite DB", Toast.LENGTH_SHORT).show();
}
});
}
private void sqlite() {
// TODO Auto-generated method stub
sqlite_obj.open();
sqlite_obj.deleteAll();
for(int i=0; i<list1.size(); i++) {
sqlite_obj.insert(list1.get(i).toString(), list2.get(i).toString());
}
sqlite_obj.close();
}
private void webservice() {
// TODO Auto-generated method stub
try {
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(ip);
HttpResponse response = httpClient.execute(httpPost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}
catch (Exception e) {
Log.e("Webservice 1", e.toString());
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
while((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result = sb.toString();
}
catch (Exception e) {
Log.e("Webservice 2", e.toString());
}
try {
JSONArray ja = new JSONArray(result);
JSONObject jo = null;
list1 = new ArrayList<String>();
list2 = new ArrayList<String>();
for(int i=0; i<ja.length(); i++) {
jo = ja.getJSONObject(i);
list1.add(jo.getString("id"));
list2.add(jo.getString("uname"));
}
}catch (Exception e) {
Log.e("Webservice 3", e.toString());
}
}
}
SQliteDB
public class SQLiteDB {
public static final String KEY_ID = "id";
public static final String KEY_NAME = "name";
private static final String TAG = "DBAdapter";
private static final String DATABASE_NAME = "SQLiteDB";
private static final String DATABASE_TABLE = "sample";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE =
"create table sample (id text primary key, name text not null);";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;
public SQLiteDB(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(DATABASE_CREATE);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS sample");
onCreate(db);
}
}
//---open SQLite DB---
public SQLiteDB open() throws SQLException {
db = DBHelper.getWritableDatabase();
return this;
}
//---close SQLite DB---
public void close() {
DBHelper.close();
}
//---insert data into SQLite DB---
public long insert(String id, String name) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_ID, id);
initialValues.put(KEY_NAME, name);
return db.insert(DATABASE_TABLE, null, initialValues);
}
//---Delete All Data from table in SQLite DB---
public void deleteAll() {
db.delete(DATABASE_TABLE, null, null);
}
}
logcat的
07-16 15:05:52.811: D/memalloc(31042): ion: Mapped buffer base:0x547b0000 size:122880 offset:0 fd:79
07-16 15:05:52.811: D/memalloc(31042): ion: Mapped buffer base:0x507ad000 size:4096 offset:0 fd:81
07-16 15:05:54.731: D/memalloc(31042): ion: Unmapping buffer base:0x547b0000 size:122880
07-16 15:05:54.731: D/memalloc(31042): ion: Unmapping buffer base:0x507ad000 size:4096
07-16 15:05:57.241: W/dalvikvm(31042): threadid=1: thread exiting with uncaught exception ( group=0x41815540)
07-16 15:05:57.251: E/AndroidRuntime(31042): FATAL EXCEPTION: main
07-16 15:05:57.251: E/AndroidRuntime(31042): java.lang.NullPointerException
07-16 15:05:57.251: E/AndroidRuntime(31042): at balaji.sqlite_webservice.MainActivity.sqlite( MainActivity.java:81)
07-16 15:05:57.251: E/AndroidRuntime(31042): at balaji.sqlite_webservice.MainActivity.access$1( MainActivity.java:74)
07-16 15:05:57.251: E/AndroidRuntime(31042): at balaji.sqlite_webservice.MainActivity$2.onClick( MainActivity.java:68)
07-16 15:05:57.251: E/AndroidRuntime(31042): at android.view.View.performClick(View.java:4102)
07-16 15:05:57.251: E/AndroidRuntime(31042): at android.view.View$PerformClick.run(View.java:17085)
07-16 15:05:57.251: E/AndroidRuntime(31042): at android.os.Handler.handleCallback(Handler.java:615)
07-16 15:05:57.251: E/AndroidRuntime(31042): at android.os.Handler.dispatchMessage(Handler.java:92)
07-16 15:05:57.251: E/AndroidRuntime(31042): at android.os.Looper.loop(Looper.java:155)
07-16 15:05:57.251: E/AndroidRuntime(31042): at android.app.ActivityThread.main(ActivityThread. java:5520)
07-16 15:05:57.251: E/AndroidRuntime(31042): at java.lang.reflect.Method.invokeNative(Native Method)
07-16 15:05:57.251: E/AndroidRuntime(31042): at java.lang.reflect.Method.invoke(Method.java:511)
07-16 15:05:57.251: E/AndroidRuntime(31042): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller .run(ZygoteInit.java:1029)
07-16 15:05:57.251: E/AndroidRuntime(31042): at com.android.internal.os.ZygoteInit.main(ZygoteInit. java:796)
07-16 15:05:57.251: E/AndroidRuntime(31042): at dalvik.system.NativeStart.main(Native Method)