这是我的RegisterActivity.java:
package com.example.loginactivity;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends Activity implements OnClickListener
{
private EditText mid, mpass, mnumber;
private Button mRegister;
private ProgressDialog pDialog;
JSONParser jsonParser = new JSONParser();
private static final String LOGIN_URL = "http://192.168.0.101:8080/trialandroid1/registered.jsp";
private static final String TAG_SUCCESS = "success";
private static final String TAG_MESSAGE = "message";
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
mid = (EditText)findViewById(R.id.id);
mpass = (EditText)findViewById(R.id.password);
mnumber=(EditText)findViewById(R.id.number);
mRegister = (Button)findViewById(R.id.btnRegister);
mRegister.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
new CreateUser().execute();
}
class CreateUser extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
boolean failure = false;
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(RegisterActivity.this);
pDialog.setMessage("Registreing...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
@Override
protected String doInBackground(String... args) {
// TODO Auto-generated method stub
// Check for success tag
int success;
String id1=((EditText)findViewById(R.id.id)).getText().toString();
String password1=((EditText)findViewById(R.id.password)).getText().toString();
String number1=((EditText)findViewById(R.id.number)).getText().toString();
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id", id1));
params.add(new BasicNameValuePair("password", password1));
params.add(new BasicNameValuePair("number",number1));
Log.d("request!", "starting");
//Posting user data to script
JSONObject json = jsonParser.makeHttpRequest(
LOGIN_URL, "POST", params);
// full json response
Log.d("Login attempt", json.toString());
// json success element
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
Log.d("User Created!", json.toString());
finish();
return json.getString(TAG_MESSAGE);
}else{
Log.d("Login Failure!", json.getString(TAG_MESSAGE));
return json.getString(TAG_MESSAGE);
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(String file_url) {
// dismiss the dialog once product deleted
pDialog.dismiss();
if (file_url != null){
Toast.makeText(RegisterActivity.this, file_url, Toast.LENGTH_LONG).show();
}
}
}
}
我的Register.xml是:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout
android:background="@color/blue"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<!-- Header Starts-->
<LinearLayout
android:id="@+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@layout/header_gradient"
android:orientation="vertical"
android:paddingBottom="5dip"
android:paddingTop="5dip" >
<TextView
android:id="@+id/myText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.53"
android:gravity="center"
android:lines="1"
android:text="WELCOME TO SVIT APP"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="sans"
android:textColor="@color/myText"
android:ellipsize="marquee"
android:marqueeRepeatLimit="marquee_forever"
android:singleLine="true"
android:fadingEdge="horizontal"
android:scrollHorizontally="true"
android:paddingLeft="15dip"
android:paddingRight="15dip"
android:focusable="true"
android:clickable="true"
android:focusableInTouchMode="true"
android:freezesText="true"/>
<!-- Logo Start-->
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:contentDescription="@string/app_name"
android:src="@drawable/logo" />
<!-- Logo Ends -->
</LinearLayout>
<!-- Header Ends -->
<!-- Footer Start -->
<!-- Footer Ends -->
<!-- Registration Form -->
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:layout_below="@id/header">
<!-- Full Name Label -->
<!-- Email Label -->
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="Login ID:"/>
<!-- Password Label -->
<EditText
android:id="@+id/id"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="Password"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPassword" />
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#372c24"
android:text="Contact_no."/>
<EditText
android:id="@+id/number"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="text" />
<Button android:id="@+id/btnRegister"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Register New Account"/>
<!-- Link to Login Screen -->
</LinearLayout>
<!-- Registration Form Ends -->
</RelativeLayout>
</ScrollView>
我的registered.jsp是:
<%@page import="java.sql.*"%>
<%
String uname=request.getParameter("username");
String pass= request.getParameter("password");
String contact=request.getParameter("contact");
try {
Connection con = null;
String connectionURL = "jdbc:mysql://localhost:3306/userreg";
Class.forName("com.mysql.jdbc.Driver").newInstance();
con = DriverManager.getConnection(connectionURL, "root", "123456");
//out.println("HI.");
Statement s = null;
s = con.createStatement();
String s1 = "insert into login(username,password,phone) values('"+uname+"','"+pass+"','"+contact+"')";
s.executeUpdate(s1);
// out.println("HI.");
out.println("{'status':'success'}");
}
catch (Exception e) {
e.printStackTrace();
}
%>
我从Android应用程序注册时遇到上述错误。 logcat是:
03-30 15:51:20.958: W/IInputConnectionWrapper(23142): showStatusIcon on inactive InputConnection
03-30 15:53:19.808: D/request!(23142): starting
03-30 15:53:20.138: E/JSON Parser(23142): Error parsing data org.json.JSONException: End of input at character 1 of
03-30 15:53:20.138: W/dalvikvm(23142): threadid=11: thread exiting with uncaught exception (group=0xb2bf9180)
03-30 15:53:20.138: I/Process(23142): Sending signal. PID: 23142 SIG: 9
03-30 15:53:20.138: D/AndroidRuntime(23142): procName from cmdline: com.example.loginactivity
03-30 15:53:20.138: E/AndroidRuntime(23142): in writeCrashedAppName, pkgName :com.example.loginactivity
03-30 15:53:20.138: D/AndroidRuntime(23142): file written successfully with content: com.example.loginactivity StringBuffer : ;com.example.loginactivity
03-30 15:53:20.138: E/AndroidRuntime(23142): FATAL EXCEPTION: AsyncTask #1
03-30 15:53:20.138: E/AndroidRuntime(23142): java.lang.RuntimeException: An error occured while executing doInBackground()
03-30 15:53:20.138: E/AndroidRuntime(23142): at android.os.AsyncTask$3.done(AsyncTask.java:278)
03-30 15:53:20.138: E/AndroidRuntime(23142): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-30 15:53:20.138: E/AndroidRuntime(23142): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-30 15:53:20.138: E/AndroidRuntime(23142): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-30 15:53:20.138: E/AndroidRuntime(23142): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-30 15:53:20.138: E/AndroidRuntime(23142): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
03-30 15:53:20.138: E/AndroidRuntime(23142): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-30 15:53:20.138: E/AndroidRuntime(23142): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-30 15:53:20.138: E/AndroidRuntime(23142): at java.lang.Thread.run(Thread.java:856)
03-30 15:53:20.138: E/AndroidRuntime(23142): Caused by: java.lang.NullPointerException
03-30 15:53:20.138: E/AndroidRuntime(23142): at com.example.loginactivity.RegisterActivity$CreateUser.doInBackground(RegisterActivity.java:118)
03-30 15:53:20.138: E/AndroidRuntime(23142): at com.example.loginactivity.RegisterActivity$CreateUser.doInBackground(RegisterActivity.java:1)
03-30 15:53:20.138: E/AndroidRuntime(23142): at android.os.AsyncTask$2.call(AsyncTask.java:264)
03-30 15:53:20.138: E/AndroidRuntime(23142): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-30 15:53:20.138: E/AndroidRuntime(23142): ... 5 more
答案 0 :(得分:0)
您似乎没有从JSP脚本返回json。你现在已经评论了 &#39; HI&#39;,我之前提到的,这不是一个有效的json。您应该从jsp脚本返回一个有效的json响应,如下所示。
为了成功
{"status":"success"}
失败
{"status":"failed","msg":"An error message explaining what went wrong"}
如需帮助,请参阅以下有关从jsp
返回json的答案Return JSON from one JSP to another?
Returning JSON response from Servlet to Javascript/JSP page
我希望它会对你有所帮助。
在您的jsp脚本中,检查是否有任何参数为空或者没有有效值,然后返回标识错误的json,并且如果在执行查询时出现任何问题,请检查并返回有助于用户的消息。如果一切顺利,只需返回成功消息。