我尝试将json数据和图像存储到sqlite,我的图像使用类型blob,我的json数据字符串成功但我的图像失败或null,我该怎么办?
LogCat错误
01-03 04:22:54.827: W/System.err(22776): java.io.FileNotFoundException: http://10.0.2.2:81/bantulfolder/sqlitepk1.JPG
01-03 04:22:54.846: W/System.err(22776): at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
01-03 04:22:54.846: W/System.err(22776): at com.dbp.insertalldata.MainActivity.getLogoImage(MainActivity.java:138)
01-03 04:22:54.865: W/System.err(22776): at com.dbp.insertalldata.MainActivity.access$2(MainActivity.java:133)
01-03 04:22:54.865: W/System.err(22776): at com.dbp.insertalldata.MainActivity$JSONParse.onPostExecute(MainActivity.java:115)
01-03 04:22:54.865: W/System.err(22776): at com.dbp.insertalldata.MainActivity$JSONParse.onPostExecute(MainActivity.java:1)
01-03 04:22:54.865: W/System.err(22776): at android.os.AsyncTask.finish(AsyncTask.java:417)
01-03 04:22:54.865: W/System.err(22776): at android.os.AsyncTask.access$300(AsyncTask.java:127)
01-03 04:22:54.876: W/System.err(22776): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
我在下面的代码活动
public class MainActivity extends Activity {
Button Btngetdata;
// URL to get JSON Array
private static String url = "http://10.0.2.2:81/bantulfolder/alldata.php";
private String IMAGEURL = "http://10.0.2.2:81/bantulfolder/";
private byte[] logoImage;
byte[] gambarku;
// JSON Node Names
private static final String TAG_DATA = "data";
public static final String TAG_IDT = "idt";
public static final String TAG_IDL = "idl";
public static final String TAG_NAMA = "nama";
public static final String TAG_ALAMAT = "alamat";
public static final String TAG_GAMBAR = "gambar";
public static final String TAG_DESKRIPSI = "deskripsi";
public static final String TAG_LATITUDE = "latitude";
public static final String TAG_LONGTITUDE = "longtitude";
JSONArray user = null;
private DatabaseHandler databaseHelper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
databaseHelper = new DatabaseHandler(MainActivity.this);
Btngetdata = (Button) findViewById(R.id.getdata);
Btngetdata.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
new JSONParse().execute();
}
});
}
private class JSONParse extends AsyncTask<String, String, JSONObject> {
final String TAG = "AsyncTaskParseJson.java";
private ProgressDialog pDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(MainActivity.this);
pDialog.setMessage("Getting Data ...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected JSONObject doInBackground(String... args) {
JSONParser jParser = new JSONParser();
// Getting JSON from URL
JSONObject json = jParser.getJSONFromUrl(url);
return json;
}
@Override
protected void onPostExecute(JSONObject json) {
pDialog.dismiss();
try {
// Getting JSON Array
user = json.getJSONArray(TAG_DATA);
// loop through all users
for (int i = 0; i < user.length(); i++) {
JSONObject c = user.getJSONObject(i);
// Storing JSON item in a Variable
String idt = c.getString(TAG_IDT);
String idl = c.getString(TAG_IDL);
String nama = c.getString(TAG_NAMA);
String alamat = c.getString(TAG_ALAMAT);
String gambar = c.getString(TAG_GAMBAR);
String deskripsi = c.getString(TAG_DESKRIPSI);
String latitude = c.getString(TAG_LATITUDE);
String longtitude = c.getString(TAG_LONGTITUDE);
logoImage = getLogoImage(IMAGEURL+gambar);
databaseHelper.saveCategoryRecord(idt, idl, nama, alamat, gambar, logoImage, deskripsi, latitude, longtitude);
// Set JSON Data in TextView
// uid.setText(idt);
//name1.setText(name);
// email1.setText(email);
// show the values in our logcat
Log.e(TAG, "nama >>>> " + idt + " alamat>>>>> " + nama);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}
数据处理程序,我用于创建表并保存图像
public class DatabaseHandler {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "bantul";
private static final String TABLE_NAME = "tb_alldata";
public static final String TAG_IDT = "idt";
public static final String TAG_IDL = "idl";
public static final String TAG_NAMA = "nama";
public static final String TAG_ALAMAT = "alamat";
public static final String TAG_GAMBAR = "gambar";
public static final String TAG_FILE_GAMBAR = "file_gambar";
public static final String TAG_DESKRIPSI = "deskripsi";
public static final String TAG_LATITUDE = "latitude";
public static final String TAG_LONGTITUDE = "longtitude";
Category openHelper;
private SQLiteDatabase database;
public DatabaseHandler(Context context){
openHelper = new Category(context);
database = openHelper.getWritableDatabase();
}
public void saveCategoryRecord(String idt, String idl, String nama, String alamat, String gambar, byte[] logoImage,
String deskripsi, String latitude, String longtitude) {
ContentValues contentValues = new ContentValues();
contentValues.put(TAG_IDT, idt);
contentValues.put(TAG_IDL, idl);
contentValues.put(TAG_NAMA, nama);
contentValues.put(TAG_ALAMAT, alamat);
contentValues.put(TAG_GAMBAR, gambar);
contentValues.put(TAG_FILE_GAMBAR, logoImage);
contentValues.put(TAG_DESKRIPSI, deskripsi);
contentValues.put(TAG_LATITUDE, latitude);
contentValues.put(TAG_LONGTITUDE, longtitude);
database.insert(TABLE_NAME, null, contentValues);
}
public Cursor getTimeRecordList() {
return database.rawQuery("select * from " + TABLE_NAME, null);
}
private class Category extends SQLiteOpenHelper {
public Category(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
db.execSQL("CREATE TABLE " + TABLE_NAME + "( "
+ TAG_IDT + " INTEGER PRIMARY KEY, "
+ TAG_IDL + " INTEGER, "+ TAG_NAMA + " TEXT, "+ TAG_ALAMAT + " TEXT, "+ TAG_GAMBAR + " TEXT, "
+ TAG_FILE_GAMBAR + " BLOB, "+ TAG_DESKRIPSI + " TEXT, " + TAG_LATITUDE + " INTEGER, "
+ TAG_LONGTITUDE + " INTEGER )" );
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABLE IF EXISTS"+ TABLE_NAME);
onCreate(db);
}
}
}
和类JSONParser解析器json数据,请帮帮我
答案 0 :(得分:1)