安卓连接数据库问题
2017-11-30 00:00
183 查看
摘要: 安卓项目通过WebService连接数据库时,出现错误
明明之前的项目还是可以连接数据库的,把之前的相关代码导入新的项目中,并没有报错。但是就是连接不上数据库,看了一眼Logcat,里面写了很多网络什么什么的错误,太多了而且也没太看懂。(听见老师说了一句“这就是个问题”,才恍然大悟,其实这个点挺奇怪的。哈哈)既然需要联网传递参数,那么Manifest.xml文件就一定是要设置允许网络连接。可是所有的java文件都导入以后,我竟然忘记了。所以才会出现一点错误提示都没有,但是就是不能传递参数到WebService。
所以即使没太看懂为什么出现了问题,但是在Manifest尝试添加了允许网络连接的代码(不对的话就再找问题嘛,幸运的是我猜对了),就可以连接数据库了。
附上我的LoginActivity.java文件
再附上我的activity_login.xml文件
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>
相关文章推荐
- 问题描述:客户端连接数据库报错,ORA-12516: TNS: 监听程序无法找到匹配协议栈的可用句柄
- Mybatis 配置数据库连接properites文件的问题
- 【CSDN常见问题解答】Java多种数据库连接
- 使用pyMySQL连接数据库时遇到的几个问题
- jsp连接数据库问题
- 解决oracle关于OEM 中“Enterprise Manager 无法连接到数据库实例”的问题
- arcgis连接postgre数据库问题
- 关于利用JDBC连接sqlserver中数据库的问题
- TL-WR720N路由器Android安卓不能连接外网问题解决
- oracle11g无法连接到数据库故障排除(win7)用户名口令问题
- PHP REDIS 使用长连接多数据库存储到最后一个数据库中的问题解决
- jboss 的一些配置(端口,虚拟目录,虚拟主机,中文问题,数据库连接)
- JDBC连接数据库问题
- 动软.Net代码生成器无法连接数据库问题!
- 关于 Oracle 连接数据库 二次登陆问题
- 最近在配mysql和web分离的服务器时所出现的phpmyadmin无法连接远程数据库问题
- kettle以JNDI的方式连接数据库&&乱码问题
- 关于ADO.NET连接数据库的问题
- Oracle10GODP连接11G数据库,出现ORA - 1017用户名/口令无效; 登录被拒绝 的问题
- ArcGIS 10.2.2 for Desktop连接Oracle(2014年10月发布)数据库崩溃的问题