- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
您好,我对 Android 开发非常陌生。我在使用 JSP 连接 Android 与 MySQL 数据库时遇到问题。我读了很多文档,但我不知道如何解决这个问题。我不确定,但我认为问题与空指针问题有关。但我不知道它在哪里......
这是我的java代码。
主Activity.java
package com.example.user.nacho_http_connection_test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class MainActivity extends Activity {
EditText edit_id, edit_pw;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edit_id = (EditText) findViewById(R.id.login_id);
edit_pw = (EditText) findViewById(R.id.login_pw);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
public void join_btn(View view) {
Intent intent;
intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
public void login_btn(View view) {
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
String sMessage = edit_id.getText().toString();
String result = SendByHttp(sMessage);
String[][] parsedData = jsonParserList(result);
if (parsedData[0][0].equals("succed")) {
Toast.makeText(MainActivity.this, "Login Success", Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, LoginActivity.class);
startActivity(intent);
finish();
}
}
private String SendByHttp(String msg) {
if (msg == null) {
msg = "";
}
String URL = "http://IP address/Test_Json/User.jsp";
DefaultHttpClient client = new DefaultHttpClient();
try {
HttpPost post = new HttpPost(URL + "?id=" + edit_id.getText().toString()+"&pw="+edit_pw.getText().toString());
HttpResponse response = client.execute(post);
BufferedReader bufreader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "utf-8"));
String line = null;
String result = "";
while ((line = bufreader.readLine()) != null) {
result += line;
}
return result;
} catch (Exception e) {
e.printStackTrace();
client.getConnectionManager().shutdown();
return "";
}
}
public String[][] jsonParserList(String pRecvServerPage){
Log.i("whole content from Server", pRecvServerPage);
try{
JSONObject json = new JSONObject(pRecvServerPage);
JSONArray jArr = json.getJSONArray("List");
String[] jsonName = {"msg1","msg2","msg3"};
String[][] parseredData = new String[jArr.length()][jsonName.length];
for(int i = 0; i<jArr.length();i++){
json = jArr.getJSONObject(i);
for (int j=0;j<jsonName.length; j++){
parseredData[i][j] = json.getString(jsonName[j]);
}
}
for(int i=0;i<parseredData.length;i++)
{
Log.i("Analyzed JSON Data"+i+":",parseredData[i][0]);
Log.i("Analyzed JSON Data"+i+":",parseredData[i][1]);
Log.i("Analyzed JSON Data"+i+":",parseredData[i][2]);
}
return parseredData;
}catch (JSONException e){
e.printStackTrace();
return null;
}
}
}
用户.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import = "org.json.simple.*"%>
<%@ page import ="java.sql.*" %>
<%
String id = request.getParameter("id");
String pw = request.getParameter("pw");
JSONObject jsonMain = new JSONObject();
JSONArray jArray = new JSONArray();
JSONObject jObject = new JSONObject();
Class.forName("com.mysql.jdbc.Driver");
try{
Connection conn = DriverManager.getConnection(
"jdbc:mysql://IP address/dbname", "admin", "1234");
Statement stmt = conn.createStatement();
String query = "select * from MEMBER";
ResultSet rs = stmt.executeQuery(query);
int i =0;
while(rs.next())
{
String _id = rs.getString("id");
String _pw = rs.getString("pw");
if(id.equals(_id)&&pw.equals(_pw)){
i =1;
jObject.put("msg1", "succed");
jObject.put("msg2", "two");
jObject.put("msg3", "three");
}
}
if(i==0){
jObject.put("msg1", "failed");
jObject.put("msg2", "two");
jObject.put("msg3", "three");
}
stmt.close();
conn.close();
jArray.add(0,jObject);
jsonMain.put("List",jArray);
out.println(jsonMain.toJSONString());
}catch(Exception e){
}
%>
日志猫
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test I/whole content from Server: <!DOCTYPE html><html><head><title>Apache Tomcat/8.0.33 - Error report</title><style type="text/css">H1 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:22px;} H2 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:16px;} H3 {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;font-size:14px;} BODY {font-family:Tahoma,Arial,sans-serif;color:black;background-color:white;} B {font-family:Tahoma,Arial,sans-serif;color:white;background-color:#525D76;} P {font-family:Tahoma,Arial,sans-serif;background:white;color:black;font-size:12px;}A {color : black;}A.name {color : black;}.line {height: 1px; background-color: #525D76; border: none;}</style> </head><body><h1>HTTP Status 404 - /Test_Json/User.jsp</h1><div class="line"></div><p><b>type</b> Status report</p><p><b>message</b> <u>/Test_Json/User.jsp</u></p><p><b>description</b> <u>The requested resource is not available.</u></p><hr class="line"><h3>Apache Tomcat/8.0.33</h3></body></html>
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: org.json.JSONException: Value <!DOCTYPE of type java.lang.String cannot be converted to JSONObject
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at org.json.JSON.typeMismatch(JSON.java:111)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at org.json.JSONObject.<init>(JSONObject.java:160)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at org.json.JSONObject.<init>(JSONObject.java:173)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at com.example.user.nacho_http_connection_test.MainActivity.jsonParserList(MainActivity.java:126)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at com.example.user.nacho_http_connection_test.MainActivity.login_btn(MainActivity.java:66)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at java.lang.reflect.Method.invoke(Native Method)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at android.view.View$1.onClick(View.java:4015)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at android.view.View.performClick(View.java:4780)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at android.view.View$PerformClick.run(View.java:19866)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at android.os.Handler.handleCallback(Handler.java:739)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at android.os.Handler.dispatchMessage(Handler.java:95)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at android.os.Looper.loop(Looper.java:135)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5254)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at java.lang.reflect.Method.invoke(Native Method)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at java.lang.reflect.Method.invoke(Method.java:372)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
05-03 09:10:48.031 3982-3982/com.example.user.nacho_http_connection_test W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-03 09:10:48.032 3982-3982/com.example.user.nacho_http_connection_test D/AndroidRuntime: Shutting down VM
--------- beginning of crash
05-03 09:10:48.032 3982-3982/com.example.user.nacho_http_connection_test E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.user.nacho_http_connection_test, PID: 3982
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to read from null array
at com.example.user.nacho_http_connection_test.MainActivity.login_btn(MainActivity.java:75)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
05-03 09:10:52.844 3982-3982/com.example.user.nacho_http_connection_test I/Process: Sending signal. PID: 3982 SIG: 9
如有任何帮助,我们将不胜感激。
最佳答案
您正在发送 HTML(在 Jsp 中)并期望在 Android Activity 中发送 JSON。确保http://IP address/Test_Json/User.jsp/"?id="+ +&pw="将返回 Json 响应。
关于java - Android-JSP org.json.JSONException : Value <! java.lang.String 类型的 DOCTYPE 无法转换为 JSONObject,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37001929/
在 Tomcat 6/Ubuntu 12.04 上启动 Grails 2.1.0 应用程序时出现以下错误。 Error 500 - Internal Server Error. groovy.lang
在运行 Storm 拓扑时,我收到此错误。拓扑完美运行 5 分钟,没有任何错误,然后失败。我正在使用 Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS as 300 sec i
我有一个 jsp 代码在其中一台机器上运行良好。但是当我复制到另一台机器时,我得到了这个 no such method found 异常。我是 Spring 的新手。有人可以解释我错过了什么吗? 以下
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我的代码在下面给出了一个错误; Exception in thread "main" java.lang.NoSuchMethodError: com/myApp/Client.cypherCBC(L
我正在尝试一个 Restful web 服务示例,所以当我要访问 url 时,我遇到了异常 java.lang.NoSuchMethodError: jersey.repackaged.com.goo
我正在将一个 Spring web 项目转换为一个 Maven 项目,但我收到了这个错误: java.lang.NoSuchMethodError: org.jboss.logging.Logger.
在我的项目中,我有一个像这样的枚举: public enum MyEnum { FIRST(1), SECOND(2); private int value; private MyEnum(int v
我创建了这个简单的示例,用于读取 Linux 正常运行时间: public String getMachineUptime() throws IOException { String[] di
我正在使用 Eclipse,并且正在使用 Java。我的目标是使用 bogoSort 方法对 vector 进行排序在一个 vector (vectorExample)中适应我的 vector 类型,
我正在运行以下查询。它显示一条错误消息。如何解决这个错误? ListrouteList=null; List companyList = session.createS
我有以下模型类: @Entity @Table(name="user_content") @org.hibernate.annotations.NamedQueries({ @org.
我有那个错误。这是我的代码: GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME
实际上我在执行我的java程序时遇到了下面提到的错误 Exception in thread "pool-1-thread-1" java.lang.ClassCastException: jav
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String 我在以下代码中遇到此异常: Strin
我正在尝试从 linkedhashset 中检索随机元素。下面是我的代码,但它每次都给我异常。 private static void generateRandomUserId(Set userIds
我已经完成了 Android 中的代码: List spinnerArray = new ArrayList(); for (int i = 0; i item = (LinkedTreeMap)
这个问题已经有答案了: Explanation of ClassCastException in Java (12 个回答) 已关闭 6 年前。 我已经编写了 java 到 Json 的代码,同时从页
这个问题在这里已经有了答案: ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn (4 个答案) 关闭 6 年前
我在运行时遇到问题来编译这段代码,这给我一个错误,java.lang.Integer 无法转换为 Java.lang.Double。如果有人帮助我更正此代码,我将非常高兴 double x; pu
我是一名优秀的程序员,十分优秀!