我想要制作的应用程序的简要说明
在线mysql数据库 - >到json对象用php代码 - >到sqlite数据库---->到listview
我是Android开发的新手,我正在尝试从在线示例和教程中学习。我将在这里发布所有项目和代码。
为了更容易找到问题我告诉你,当我运行程序时,我看到logcat消息“JSON和我的数据库的一些数据”。所以PHP运行良好。
问题是,当我运行程序时,我在模拟器上出现空白屏幕
我很乐意提供您需要的更多数据。
我想要做的是从在线mysql数据库中获取数据,并在开始屏幕上使用这些数据制作一个列表视图
我的main.xml只包含一个带有名单的列表视图。我的自定义列表包含3个带有名称dateview storeview eventview的文本视图
这是Main.java
package gr.nickKladis.gridview;
import org.json.JSONException;
import org.json.JSONObject;
import gr.nickKladis.information.library.DatabaseHelper;
import gr.nickKladis.information.library.UserFunctions;
import android.app.Activity;
import android.app.ListActivity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class Main extends Activity {
/** Called when the activity is first created. */
private static String KEY_SUCCESS = "success";
private static String KEY_ERROR = "error";
private static String KEY_ERROR_MSG = "error_msg";
private static String KEY_ID = "uid";
private static String KEY_DATE = "date";
private static String KEY_HOURS = "hours";
private static String KEY_STORENAME = "store_name";
private static String KEY_EVENTINFORMATION = "event_information";
private static String KEY_EVENTTYPE = "event_type";
private static String KEY_PHONE = "phone";
private static String KEY_ADDRESS = "address";
private static String KEY_CREATED_AT = "created_at";
protected Cursor cursor;
protected ListView eventlist;
protected ListAdapter adapter;
protected SQLiteDatabase db;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
eventlist = (ListView) findViewById (R.id.list); //declaration of the listview
UserFunctions userFunction = new UserFunctions();
JSONObject json = userFunction.showallevents();
try {
if (json.getString(KEY_SUCCESS) != null)
{
String res = json.getString(KEY_SUCCESS);
if(Integer.parseInt(res) == 1)
{
DatabaseHelper db = new DatabaseHelper(getApplicationContext()); //casted
JSONObject json_event = json.getJSONObject("event");
db.addEvent(json_event.getString(KEY_DATE), json_event.getString(KEY_HOURS), json_event.getString(KEY_STORENAME), json_event.getString(KEY_EVENTINFORMATION),json_event.getString(KEY_EVENTTYPE), json_event.getString(KEY_PHONE),json_event.getString(KEY_ADDRESS), json_event.getString(KEY_CREATED_AT));
cursor = db.getWritableDatabase().rawQuery("SELECT `events`.`store_name`,`events`.`date`,`events`.`event_type`FROM `events`", null);
adapter = new SimpleCursorAdapter( //cursor adapter
this,
R.layout.customlist,
cursor,
new String[] {"store_name", "event_type", "date"},
new int[] {R.id.storeview, R.id.eventview, R.id.dateview});
eventlist.setAdapter(adapter);
}
}
}
catch (JSONException e)
{
e.printStackTrace();
}
}
};
这是DatabaseHelper.java
package gr.nickKladis.information.library;
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "infodata"; //database name
private static final String TABLE_LOGIN = "events";
private static final String KEY_ID = "uid";
private static final String KEY_DATE = "date";
private static final String KEY_HOURS = "hours";
private static final String KEY_STORENAME = "store_name";
private static final String KEY_EVENTINFORMATION = "event_information";
private static final String KEY_EVENTTYPE = "event_type";
private static final String KEY_PHONE = "phone";
private static final String KEY_ADDRESS = "address";
private static final String KEY_CREATED_AT = "created_at";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1); //construction of database
}
@Override
public void onCreate(SQLiteDatabase db) {
String sql = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY,"
+ KEY_DATE + " DATE,"
+ KEY_HOURS + " TEXT,"
+ KEY_STORENAME + " TEXT,"
+ KEY_EVENTINFORMATION + " TEXT,"
+ KEY_EVENTTYPE + " TEXT,"
+ KEY_PHONE + " INTEGER,"
+ KEY_ADDRESS + " TEXT,"
+ KEY_CREATED_AT + " TEXT" + ")";
db.execSQL(sql); //execution of the sql string
}
public void addEvent(String date , String hours, String storename, String eventinformation, String eventtype, String phone, String address, String created_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_DATE, date); // Name
values.put(KEY_HOURS, hours); // Hours
values.put(KEY_STORENAME, storename); // store name
values.put(KEY_EVENTINFORMATION, eventinformation); // event information
values.put(KEY_EVENTTYPE, eventtype); // event type
values.put(KEY_PHONE, phone); // phone
values.put(KEY_ADDRESS, address); // address
values.put(KEY_CREATED_AT, created_at); // Created At
// Inserting Row
db.insert(TABLE_LOGIN, null, values);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS events"); //upgrade?
onCreate(db);
}
}
这是JSONParser.java
package gr.nickKladis.information.library;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public JSONObject getJSONFromUrl(String url, List<NameValuePair> params) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "n");
}
is.close();
json = sb.toString();
Log.e("JSON", json);
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
// return JSON String
return jObj;
}
}
这是UserFunctions.java
package gr.nickKladis.information.library;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
public class UserFunctions {
private JSONParser jsonParser;
private static String loginURL = "http://10.0.2.2/information/";
private static String login_tag = "showallevents";
public UserFunctions(){
jsonParser = new JSONParser();
}
public JSONObject showallevents(){
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("tag", login_tag));
JSONObject json = jsonParser.getJSONFromUrl(loginURL, params);
// return json
return json;
}
}
答案 0 :(得分:0)
http://grail.cba.csuohio.edu/~matos/notes/cis-493/lecture-notes/Android-Chapter10-WebKit.pdf 如果您将URL设置为其页面依赖于您的Javascript的站点 可能会看到一个空的白色屏幕。 默认情况下,在WebView小部件中关闭Javascript。 如果要启用Javascript,请致电: myWebView.setSettings()setJavaScriptEnabled(真)。 在WebView实例上