-3

我想要制作的应用程序的快速描述。
在线 mysql 数据库 --> 到带有 php 代码的 json 对象 --> 到 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;
}


}
4

1 回答 1

0

http://grail.cba.csuohio.edu/~matos/notes/cis-493/lecture-notes/Android-Chapter10-WebKit.pdf 如果您将 URL 设置为页面依赖于 Javascript 的站点,您可能会看到一个空的,白屏。默认情况下,WebView 小部件中的 Javascript 是关闭的。如果要启用 Javascript,请调用:myWebView.setSettings().setJavaScriptEnabled(true); 在 WebView 实例上

于 2012-06-07T14:44:05.203 回答