您的位置:首页 > 数据库

安卓连接数据库问题

2017-11-30 00:00 183 查看
摘要: 安卓项目通过WebService连接数据库时,出现错误

11-30 19:10:25.444 795-795/? E/OppoExService: Failure IOppoGestureCallBack onDealGesture
android.os.DeadObjectException
at android.os.BinderProxy.transact(Native Method)
at android.os.IOppoGestureCallBack$Stub$Proxy.onDealGesture(IOppoGestureCallBack.java:80)
at com.android.server.oppo.OppoExService.dealScreenoffGesture(OppoExService.java:231)
at com.android.internal.policy.impl.OppoScreenOffGestureManager.screenTurnedOff(OppoScreenOffGestureManager.java:145)
at com.android.internal.policy.impl.OppoPhoneWindowManager.screenTurnedOff(OppoPhoneWindowManager.java:1462)
at com.android.server.power.Notifier.sendGoToSleepBroadcast(Notifier.java:552)
at com.android.server.power.Notifier.sendNextBroadcast(Notifier.java:468)
at com.android.server.power.Notifier.access$500(Notifier.java:66)
at com.android.server.power.Notifier$NotifierHandler.handleMessage(Notifier.java:618)
at android.os.Handler.dispatchMessage(Handler.java:110)
at android.os.Looper.loop(Looper.java:222)
at com.android.server.ServerThread.initAndLoop(SystemServer.java:1786)
at com.android.server.SystemServer.main(SystemServer.java:1881)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)
at dalvik.system.NativeStart.main(Native Method)
11-30 19:10:25.641 795-836/? E/NetworkStats: success. readNetworkStatsDetail size = 88
11-30 19:10:25.641 795-836/? E/NetworkStats: success. readNetworkStatsDetailWithPids end.
11-30 19:10:25.650 20967-20967/? E/dalvikvm: /system/app/ColorUIEngine.apk odex has stale dependencies
11-30 19:10:25.651 795-836/? E/NetworkStats: success. readNetworkStatsDetailWithPids size = 40
11-30 19:10:25.651 795-836/? E/NetworkStats: success. readNetworkStatsDetailWithPids end.
11-30 19:10:25.746 1072-1082/? E/SQLiteLog: (1) near "type": syntax error
11-30 19:10:25.748 1072-1082/? E/DatabaseUtils: Writing exception to parcel
android.database.sqlite.SQLiteException: near "type": syntax error (code 1): , while compiling: type=1 AND read=0
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1364)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1303)
at com.android.providers.telephony.MmsSmsProvider.query(MmsSmsProvider.java:1307)
at android.content.ContentProvider.query(ContentProvider.java:922)
at android.content.ContentProvider$Transport.query(ContentProvider.java:213)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
11-30 19:10:25.763 1072-5414/? E/SQLiteLog: (1) near "msg_box": syntax error
11-30 19:10:25.764 1072-5414/? E/DatabaseUtils: Writing exception to parcel
android.database.sqlite.SQLiteException: near "msg_box": syntax error (code 1): , while compiling: msg_box=1 AND read=0
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1364)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1303)
at com.android.providers.telephony.MmsSmsProvider.query(MmsSmsProvider.java:1307)
at android.content.ContentProvider.query(ContentProvider.java:922)
at android.content.ContentProvider$Transport.query(ContentProvider.java:213)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:112)
at android.os.Binder.execTransact(Binder.java:404)
at dalvik.system.NativeStart.run(Native Method)
11-30 19:10:25.919 795-795/? E/AudioService: config_camera_sound_forced= false
11-30 19:10:25.929 1030-1030/? E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/data/com.sohu.inputmethod.sogouoem/files/.theme/sogou_custom/res/composing_bg.png: open failed: ENOENT (No such file or directory)
11-30 19:10:25.931 1030-1030/? E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /data/data/com.sohu.inputmethod.sogouoem/files/.theme/sogou_custom/res/composing_bg.png: open failed: ENOENT (No such file or directory)
11-30 19:10:25.950 21944-21944/? E/WindowManager: android.view.WindowLeaked: Activity com.example.myproject.LoginActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{42af2ea0 V.E..... R......D 0,0-1248,288} that was originally added here
at android.view.ViewRootImpl.<init>(ViewRootImpl.java:480)
at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:250)
at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
at android.app.Dialog.show(Dialog.java:318)
at android.app.ProgressDialog.show(ProgressDialog.java:116)
at android.app.ProgressDialog.show(ProgressDialog.java:99)
at android.app.ProgressDialog.show(ProgressDialog.java:94)
at com.example.myproject.LoginActivity.login(LoginActivity.java:103)
at com.example.myproject.LoginActivity.handleLogin(LoginActivity.java:97)
at com.example.myproject.LoginActivity.onClick(LoginActivity.java:84)
at android.view.View.performClick(View.java:4488)
at android.view.View$PerformClick.run(View.java:18860)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:103)
at android.os.Looper.loop(Looper.java:222)
at android.app.ActivityThread.main(ActivityThread.java:5484)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:676)
at dalvik.system.NativeStart.main(Native Method)

明明之前的项目还是可以连接数据库的,把之前的相关代码导入新的项目中,并没有报错。但是就是连接不上数据库,看了一眼Logcat,里面写了很多网络什么什么的错误,太多了而且也没太看懂。(听见老师说了一句“这就是个问题”,才恍然大悟,其实这个点挺奇怪的。哈哈)既然需要联网传递参数,那么Manifest.xml文件就一定是要设置允许网络连接。可是所有的java文件都导入以后,我竟然忘记了。所以才会出现一点错误提示都没有,但是就是不能传递参数到WebService。

所以即使没太看懂为什么出现了问题,但是在Manifest尝试添加了允许网络连接的代码(不对的话就再找问题嘛,幸运的是我猜对了),就可以连接数据库了。

<uses-permission android:name="android.permission.INTERNET"></uses-permission>

附上我的LoginActivity.java文件

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
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.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class LoginActivity extends Activity implements OnClickListener {
private EditText loginUsername;
private EditText loginPassword;
private Button loginButton;
private Button createButton;

private ProgressDialog loginProgress;

public static final int MSG_LOGIN_RESULT = 0;

public String serverUrl = "http://xxx.xx.xxx.x:8080/xxx/LoadMessage";

private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch(msg.what) {
case MSG_LOGIN_RESULT:
loginProgress.dismiss();
JSONObject json = (JSONObject) msg.obj;
handleLoginResult(json);
break;
}
};
};

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
setContentView(R.layout.activity_login);

initViews();
}
private void initViews() {
loginUsername = (EditText)findViewById(R.id.login_username);
loginPassword = (EditText)findViewById(R.id.login_password);
loginButton   = (Button)findViewById(R.id.login);
createButton  = (Button)findViewById(R.id.create_count);

loginButton.setOnClickListener(this);
createButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.login:
handleLogin();
break;
case R.id.create_count:
handleCreateCount();
break;
default:
break;
}

}
private void handleLogin() {
String username = loginUsername.getText().toString();
String password = loginPassword.getText().toString();
login(username, password);
}
private void login(final String username, final String password) {
loginProgress = new ProgressDialog(this);
loginProgress.setCancelable(false);
loginProgress.setCanceledOnTouchOutside(false);
loginProgress.show(this, null, username+password);
new Thread(new Runnable() {
@Override
public void run() {
Log.d("哈哈。。。。。。。。。。", "正在处理网络连接.......");

// 创建默认的HttpClient实例.
HttpClient httpclient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost(serverUrl);
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("username", username));
params.add(new BasicNameValuePair("password", password));

HttpResponse httpResponse = null;
try {
httpPost.setEntity(new UrlEncodedFormEntity(params, "UTF_8"));
httpResponse = httpclient.execute(httpPost);
if(httpResponse.getStatusLine().getStatusCode() == 200) {
Log.d("xiaoxiaoyu", "network OK!");
HttpEntity entity = httpResponse.getEntity();
String entityString = EntityUtils.toString(entity);
String jsonString = entityString.substring(entityString.indexOf("{"));
Log.d("xiaoxiaoyu", "entity = " + jsonString);
JSONObject json = new JSONObject(jsonString);
sendMessage(MSG_LOGIN_RESULT, json);
Log.d("xiaxiaoyu", "json = " + json);
}
} catch (UnsupportedEncodingException e) {
Log.d("哈哈。。。。。。。。。。", "UnsupportedEncodingException");
e.printStackTrace();
} catch (ClientProtocolException e) {
Log.d("哈哈。。。。。。。。。。", "ClientProtocolException");
e.printStackTrace();
} catch (IOException e) {
Log.d("哈哈。。。。。。。。。。", "IOException");
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
Log.d("哈哈。。。。。。。。。。", "IOException");
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();

}
private void handleCreateCount() {
Intent intent = new Intent(this,RegisterActivity.class);
startActivity(intent);
finish();
}

private void handleLoginResult(JSONObject json){
/*
* login_result:
* -1:登录失败,未知错误!
* 0: 登录成功!
* 1:登录失败,用户名或密码错误!
* 2:登录失败,用户名不存在!
* */
int resultCode = -1;
try {
resultCode = json.getInt("result_code");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
switch(resultCode) {
case 0:
onLoginSuccess(json);
break;
case 1:
Toast.makeText(this, "用户名或密码错误!", Toast.LENGTH_LONG).show();
break;
case 2:
Toast.makeText(this, "用户名不存在!", Toast.LENGTH_LONG).show();
break;
case -1:
default:
Toast.makeText(this, "登录失败!未知错误!", Toast.LENGTH_LONG).show();
break;
}
}

private void onLoginSuccess(JSONObject json) {
Intent intent = new Intent(this, GameActivity.class);

try {
intent.putExtra("username", json.getString("username"));
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startActivity(intent);
finish();
}
private void sendMessage(int what, Object obj) {
Message msg = Message.obtain();
msg.what = what;
msg.obj = obj;
mHandler.sendMessage(msg);
}
}

再附上我的activity_login.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@mipmap/bg2"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.myproject.LoginActivity" >
<TextView
android:id="@+id/txtSound"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="211dp"
android:text="用户名:"
android:textSize="18sp"
android:layout_marginStart="211dp"
android:layout_above="@+id/login_password"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="24dp" />

<EditText
android:id="@+id/login_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/txtSound"
android:layout_alignBottom="@+id/txtSound"
android:layout_marginLeft="16dp"
android:layout_toRightOf="@+id/txtSound"
android:ems="10" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/login_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword"
android:layout_centerVertical="true"
android:layout_alignLeft="@+id/login_username"
android:layout_alignStart="@+id/login_username" />

<Button
android:id="@+id/create_count"
android:layout_width="100dp"
android:layout_height="38dp"
android:text="点击注册"
android:textSize="12sp"
android:layout_alignRight="@+id/login_password"
android:layout_alignEnd="@+id/login_password"
android:layout_alignTop="@+id/login" />

<TextView
android:id="@+id/txtWrite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密码:"
android:textSize="18sp"
android:layout_alignBaseline="@+id/login_password"
android:layout_alignBottom="@+id/login_password"
android:layout_toLeftOf="@+id/login_username"
android:layout_toStartOf="@+id/login_username" />

<Button
android:id="@+id/login"
android:layout_width="100dp"
android:layout_height="38dp"
android:text="点击登录"
android:textSize="12sp"
android:layout_marginTop="17dp"
android:layout_below="@+id/login_password"
android:layout_alignLeft="@+id/login_password"
android:layout_alignStart="@+id/login_password" />

</RelativeLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息