以下にリストされているクラスでいくつかのテストを行ってきましたが、それはうまく機能します...
ボタンをクリックしてフラグメントを切り替えようとすると、クラッシュします。行 752:getActivity().getContentResolver().update(StatusProvider.CONTENT_URI_CHARTING, values, null, new String[] {StatusData.KEY_CHARTING_DATE,cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_DATE)) + ""});
しかし、なぜ失敗したのかわかりません。
public class ChartingFragment extends Fragment implements OnDateSetListener, OnClickListener {
...
public class WebPageAsync extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
String userID = null;
Log.i("PROJECTCARUSO","Fetching User");
//Get the user
Cursor cursorUser = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_USER, null, null, null, null);
cursorUser.moveToFirst();
if (cursorUser.getCount()>0 && cursorUser!=null) {
// Record found
if (cursorUser.getString(cursorUser.getColumnIndex(StatusData.KEY_USER_ID)) == null) {
Log.i("PROJECTCARUSO","KEY_USER_ID is null, need to upload the user information");
//this user has logged in with facebook but has not be synced before.
//Push user information to my online database.
ArrayList<NameValuePair> httpParams;
httpParams = new ArrayList<NameValuePair>();
httpParams.add(new BasicNameValuePair("fbid", cursorUser.getString(cursorUser.getColumnIndex(StatusData.KEY_USER_ROWID))));
httpParams.add(new BasicNameValuePair("social", cursorUser.getString(cursorUser.getColumnIndex(StatusData.KEY_USER_SOCIAL))));
httpParams.add(new BasicNameValuePair("name", cursorUser.getString(cursorUser.getColumnIndex(StatusData.KEY_USER_NAME))));
httpParams.add(new BasicNameValuePair("fname", cursorUser.getString(cursorUser.getColumnIndex(StatusData.KEY_USER_FNAME))));
httpParams.add(new BasicNameValuePair("username", cursorUser.getString(cursorUser.getColumnIndex(StatusData.KEY_USER_USERNAME))));
httpParams.add(new BasicNameValuePair("email", cursorUser.getString(cursorUser.getColumnIndex(StatusData.KEY_USER_EMAIL))));
httpParams.add(new BasicNameValuePair("picture", cursorUser.getString(cursorUser.getColumnIndex(StatusData.KEY_USER_PICTURE))));
httpParams.add(new BasicNameValuePair("other", cursorUser.getString(cursorUser.getColumnIndex(StatusData.KEY_USER_OTHER))));
try {
SERVICE_URL = new URL("http://www.projectcaruso.com/android/com.projectcaruso/naturalfamilyplaning/postUser.php");
} catch (MalformedURLException e) {
Log.i("PROJECTCARUSO", "MalformedURLException: " + e.toString());
e.printStackTrace();
}
String response= "";
try {
HttpURLConnection conn = (HttpURLConnection) SERVICE_URL.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(httpParams));
writer.close();
os.close();
conn.connect();
//start listening to the stream
Scanner inStream = new Scanner(conn.getInputStream());
//process the stream and store it in StringBuilder
while(inStream.hasNextLine())
response+=(inStream.nextLine());
Log.i("PROJECTCARUSO","response: " + response);
} catch (ProtocolException e) {
Log.i("PROJECTCARUSO", "Error: " + e.toString());
}
catch (IOException e) {
Log.i("PROJECTCARUSO", e.toString());
}
// First split the input String into an array,
// each element containing a String to be parse as an int
String[] intsToParse = response.split("\"");
int[] info = new int[intsToParse.length];
int _id = 0;
// Now just parse each part in turn
for (int i = 0; i < info.length; i++)
{
if (isNumeric(intsToParse[i].toString())) {
_id = Integer.parseInt(intsToParse[i]);
}
}
if (_id > 0) {
Log.i("PROJECTCARUSO", "Found id: " + _id);
ContentValues values = new ContentValues();
values.put(StatusData.KEY_USER_ID, _id);
getActivity().getContentResolver().update(StatusProvider.CONTENT_URI_USER, values, " 1 = 1 ", null);
Cursor cursorUser2 = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_USER, null, null, null, null);
cursorUser2.moveToFirst();
userID = cursorUser2.getString(cursorUser2.getColumnIndex(StatusData.KEY_USER_ID));
}
} else {
while (cursorUser.isAfterLast() == false)
{
//this User is already set up to send/receive charting updates
Log.i("PROJECTCARUSO","User found and has been connected before");
userID = cursorUser.getString(cursorUser.getColumnIndex(StatusData.KEY_USER_ID));
cursorUser.moveToNext();
}
}
} else {
//This user has not been set up to send/receive charting updates
Log.i("PROJECTCARUSO","User profile not set up, please login via facebook");
Log.i("PROJECTCARUSO", "userID: " + userID);
return null;
}
if (userID != null) {
// Get the data
Cursor cursorCharting = getActivity().getContentResolver().query(StatusProvider.CONTENT_URI_CHARTING, null, StatusData.KEY_CHARTING_PROC + " is null " , null, null);
cursorCharting.moveToFirst();
if (cursorCharting.getCount()>0 && cursorCharting!=null) {
Log.i("PROJECTCARUSO","Que'd Charting Information Found");
while (cursorCharting.isAfterLast() == false)
{
ArrayList<NameValuePair> httpParams;
httpParams = new ArrayList<NameValuePair>();
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_DATE, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_DATE))));
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_TEMPERATURE, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_TEMPERATURE))));
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_STAMPS, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_STAMPS))));
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_FERTILE, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_FERTILE))));
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_PERIOD, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_PERIOD))));
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_INTERCORSE, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_INTERCORSE))));
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_CERVIX, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_CERVIX))));
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_MOOD, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_MOOD))));
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_HEADACHE, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_HEADACHE))));
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_TEST, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_TEST))));
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_ENERGY, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_ENERGY))));
httpParams.add(new BasicNameValuePair(StatusData.KEY_CHARTING_NOTES, cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_NOTES))));
try {
SERVICE_URL = new URL("http://www.projectcaruso.com/android/com.projectcaruso/naturalfamilyplaning/postCharting.php");
} catch (MalformedURLException e) {
Log.i("PROJECTCARUSO", "MalformedURLException: " + e.toString());
e.printStackTrace();
}
String response= "";
try {
HttpURLConnection conn = (HttpURLConnection) SERVICE_URL.openConnection();
conn.setReadTimeout(10000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getQuery(httpParams));
writer.close();
os.close();
conn.connect();
//start listening to the stream
Scanner inStream = new Scanner(conn.getInputStream());
//process the stream and store it in StringBuilder
while(inStream.hasNextLine())
response+=(inStream.nextLine());
Log.i("PROJECTCARUSO","response: " + response);
} catch (ProtocolException e) {
Log.i("PROJECTCARUSO", "Error: " + e.toString());
}
catch (IOException e) {
Log.i("PROJECTCARUSO", e.toString());
}
// First split the input String into an array,
// each element containing a String to be parse as an int
String[] intsToParse = response.split("\"");
int[] info = new int[intsToParse.length];
int _id = 0;
// Now just parse each part in turn
for (int i = 0; i < info.length; i++)
{
if (isNumeric(intsToParse[i].toString())) {
_id = Integer.parseInt(intsToParse[i]);
}
}
if (_id >= 0) {
Log.i("PROJECTCARUSO", "Insert Successful: " + _id);
ContentValues values = new ContentValues();
values.put(StatusData.KEY_CHARTING_PROC, "Y");
getActivity().getContentResolver().update(StatusProvider.CONTENT_URI_CHARTING, values, null, new String[] {StatusData.KEY_CHARTING_DATE,cursorCharting.getString(cursorCharting.getColumnIndex(StatusData.KEY_CHARTING_DATE)) + ""});
}
cursorCharting.moveToNext();
}
} else {
Log.i("PROJECTCARUSO","No que'd Charting Information Found");
}
}
return null;
}
}
private String getQuery(List<NameValuePair> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for (NameValuePair pair : params)
{
if (pair.getValue() == null) {
Log.i("PROJECTCARUSO", "Null value pair for: " + pair.getName());
} else {
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(pair.getName(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(pair.getValue(), "UTF-8"));
}
}
return result.toString();
}
@SuppressWarnings("unused")
public static boolean isNumeric(String str) {
try
{
double d = Double.parseDouble(str);
}
catch(NumberFormatException nfe)
{
return false;
}
return true;
}
}
丸太の猫:
08-01 14:43:16.801: I/PROJECTCARUSO(23019): response: {"posts":[{"auto_increment_id":"0"}]}
08-01 14:43:16.801: I/PROJECTCARUSO(23019): Insert Successful: 0
08-01 14:43:16.811: W/dalvikvm(23019): threadid=18: thread exiting with uncaught exception (group=0x40f89360)
08-01 14:43:16.831: E/AndroidRuntime(23019): FATAL EXCEPTION: AsyncTask #4
08-01 14:43:16.831: E/AndroidRuntime(23019): java.lang.RuntimeException: An error occured while executing doInBackground()
08-01 14:43:16.831: E/AndroidRuntime(23019): at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-01 14:43:16.831: E/AndroidRuntime(23019): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-01 14:43:16.831: E/AndroidRuntime(23019): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-01 14:43:16.831: E/AndroidRuntime(23019): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
08-01 14:43:16.831: E/AndroidRuntime(23019): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
08-01 14:43:16.831: E/AndroidRuntime(23019): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
08-01 14:43:16.831: E/AndroidRuntime(23019): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
08-01 14:43:16.831: E/AndroidRuntime(23019): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
08-01 14:43:16.831: E/AndroidRuntime(23019): at java.lang.Thread.run(Thread.java:856)
08-01 14:43:16.831: E/AndroidRuntime(23019): Caused by: java.lang.IllegalArgumentException: Too many bind arguments. 3 arguments were provided but the statement needs 1 arguments.
08-01 14:43:16.831: E/AndroidRuntime(23019): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:68)
08-01 14:43:16.831: E/AndroidRuntime(23019): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
08-01 14:43:16.831: E/AndroidRuntime(23019): at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1563)
08-01 14:43:16.831: E/AndroidRuntime(23019): at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1514)
08-01 14:43:16.831: E/AndroidRuntime(23019): at com.projectcaruso.naturalfamilyplaning.StatusProvider.update(StatusProvider.java:81)
08-01 14:43:16.831: E/AndroidRuntime(23019): at android.content.ContentProvider$Transport.update(ContentProvider.java:235)
08-01 14:43:16.831: E/AndroidRuntime(23019): at android.content.ContentResolver.update(ContentResolver.java:992)
08-01 14:43:16.831: E/AndroidRuntime(23019): at com.projectcaruso.naturalfamilyplaning.ChartingFragment$WebPageAsync.doInBackground(ChartingFragment.java:752)
08-01 14:43:16.831: E/AndroidRuntime(23019): at com.projectcaruso.naturalfamilyplaning.ChartingFragment$WebPageAsync.doInBackground(ChartingFragment.java:1)
08-01 14:43:16.831: E/AndroidRuntime(23019): at android.os.AsyncTask$2.call(AsyncTask.java:287)
08-01 14:43:16.831: E/AndroidRuntime(23019): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
08-01 14:43:16.831: E/AndroidRuntime(23019): ... 5 more
08-01 14:43:24.378: D/SensorManager(23019): unregisterListener:: Trklfufi 9 budiwrd5mrfo5WirfulblrwuFmfulTrklfufi$KfukwiFmfulTrklfufiRvht@,*a-b:((