0

Here is my 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();
            }

        }

    }
}

My Register.xml is:

<?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>

My registered.jsp is:

<%@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();
                                 }


%>

I am getting the above error while registering from android app. The logcat is:

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
14
  • Have you checked the received json string ? is it a valid json string ? Commented Mar 29, 2014 at 21:45
  • I am unable to receive any json string.. Commented Mar 30, 2014 at 7:19
  • Are you sure, you are not getting any string from the web-service ? See line 5 of logcat. Commented Mar 30, 2014 at 8:50
  • yes am sure about it.. am unable to get anything Commented Mar 30, 2014 at 9:33
  • i edited my logcat output Commented Mar 30, 2014 at 10:26

1 Answer 1

0

It seems that you are not returning json from your JSP script. You have now commented the 'HI', that I had earlier mentioned, which is not a valid json. You should return a valid json response like following from your jsp script.

For success

    {"status":"success"}

For failure

    {"status":"failed","msg":"An error message explaining what went wrong"}

For your help, see the following answers for returning json from jsp

Return JSON from one JSP to another?

Returning JSON response from Servlet to Javascript/JSP page

I hope it will help you.

In your jsp script, check if any parameter is empty or has not valid value then return a json that identifies the error, and also if there occurs any problem while executing query, check and return a message that will help the user. And if all goes well simply return a success message.

Sign up to request clarification or add additional context in comments.

6 Comments

I can actually enter data from the jsp page. there is no problem in it.
For simplicity, just print out the success json string ie. {"status":"success"} just after executing query, and check your android app.
I have added a line to print json in case of success, try it now
may i know what problem are you facing now ? At the moment it is not possible, but after 4,5 hours I'll be able to help via teamviewer.
What is printed/displayed when you test this code in browser ?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.