dimanche 29 novembre 2015

Android Studio Simple SQL Database Not Working

Basically at the moment I'm creating an app that searches for movies and display a list of results and what I'm trying to currently do (but failing) is to cache searched movie results so that they load up without internet access but I'm not sure what I'm doing wrong >.<

Everytime I basically disconnect the internet and try to reload the results, the app just crashes ;_;

Search Acitivity (don't mind the typo)

package com.example.saghaf.searchmovies;

import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;



public class SearchAcitivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_search_acitivity);
    StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
    StrictMode.setThreadPolicy(policy);
}


public void goToResults(View view) {
    EditText editText = (EditText) findViewById(R.id.editText);
    String searchTerm = editText.getText().toString();
    Intent intent = new Intent(this, ResultsActivity.class);
    intent.putExtra("searchTerm", searchTerm);
    startActivity(intent);
}
}

Results Activity

package com.example.saghaf.searchmovies;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import org.json.JSONArray;
import org.json.JSONObject;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Scanner;

public class ResultsActivity extends AppCompatActivity implements        View.OnClickListener {
Button button2;

@Override
public void onCreate(Bundle savedInstanceState) {
    {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_results);
        button2 = (Button)findViewById(R.id.button2);
        button2.setOnClickListener(this);

        final ListView listView = (ListView) findViewById(R.id.listView);
        String searchTerm = getIntent().getExtras().getString("searchTerm");
        searchTerm = searchTerm.replaceAll("\\s", "+");
        String jsonResult = runSearch(searchTerm);
        ArrayList<String> resultsArray = getValuesFromJSON(jsonResult);
        ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, resultsArray);
        listView.setAdapter(stringArrayAdapter);
    }
}

private void button2Click() {
    startActivity(new Intent("android.intent.action.MAIN"));
}


public void onClick(View v) {
    Intent intent = new Intent(this, SearchAcitivity.class);
    startActivity(intent);
}

private String runSearch(String searchTerm) {
    String jsonResult = null;
    try {
        URL url = new URL("http://ift.tt/1f7siwH" + searchTerm);    //http://www.omdbapi.com/?
        HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
        InputStream inputStream = httpURLConnection.getInputStream();
        Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A");
        jsonResult = scanner.hasNext() ? scanner.next() : "";
    } catch (Exception e) {
        e.printStackTrace();
    }
    return jsonResult;
}

private ArrayList<String> getValuesFromJSON(String jsonResult) {
    ArrayList<String> resultsArray = new ArrayList<>();
    try {
        JSONObject jsonObject = new JSONObject(jsonResult);
        JSONArray jsonArray = jsonObject.getJSONArray("Search");
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject result = jsonArray.getJSONObject(i);
            StringBuilder sb = new StringBuilder();
            sb.append(result.get("Title"));
            sb.append(" - ");
            sb.append(result.get("Year"));
            resultsArray.add(sb.toString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    }

    return resultsArray;

}

}

Database

package com.example.saghaf.searchmovies;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.provider.BaseColumns;

public class Database extends SQLiteOpenHelper {

public final class FeedReaderContract {
    public FeedReaderContract() {
    }
    // To prevent someone from accidentally instantiating the contract      class,
    // I gave it an empty constructor
}

public static abstract class FeedEntry implements BaseColumns {
}

public static final String DB_NAME = "database.db";
public static final int DB_VERSION = 1;
public static final String TABLE_NAME = "database";
public static final String COL_ID = "id";
public static final String COL_JSON = "json";
private static final String TEXT_TYPE = " TEXT";
private static final String COMMA_SEP = ",";
private static final String TABLE_CREATE =
        "CREATE TABLE " + TABLE_NAME + " (" + FeedEntry._ID + " INTEGER    PRIMARY KEY," +
                ("+ COL_ID + TEXT_TYPE  + COL_JSON + COMMA_SEP +)" +
                ("+ COL_JSON + TEXT_TYPE + COMMA_SEP"));


public Database(Context context){
    super(context, DB_NAME, null, DB_VERSION);

}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

}

public void onCreate(SQLiteDatabase db) {
    db.execSQL(TABLE_CREATE);
    insertData(db);
}

public void insertData(SQLiteDatabase db){


}

}

Thanks in advance!

Aucun commentaire:

Enregistrer un commentaire