2014-03-29 44 views
0

这里是我的RegisterActivity.java:获取分析数据的错误org.json.JSONException:值HI。类型java.lang.String中不能被转换为JSONObject的

 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

你是否检查过收到的json字符串?这是一个有效的JSON字符串? – MTahir

+0

我无法收到任何json字符串.. – riachwdry23

+0

您确定,您没有从网络服务获取任何字符串吗?看到logcat的第5行。 – MTahir

回答

0

看起来你没有从JSP脚本中返回json。你现在已经评论过我刚才提到的 'HI',这不是一个有效的json。你应该从你的jsp脚本中返回一个有效的json响应,如下所示。

成功

{"status":"success"} 

对未

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

对你有所帮助,请从JSP

Return JSON from one JSP to another?

Returning JSON response from Servlet to Javascript/JSP page

返回JSON如下回答

我希望它能帮助你。

在您的jsp脚本中,检查是否有任何参数为空或者没有有效值,然后返回一个标识错误的json,并且如果在执行查询时发生任何问题,请检查并返回一条消息,以帮助用户。如果一切顺利,只需返回成功消息。

+0

@ riachwdry23看到更新的答案 – MTahir

+0

我实际上可以从jsp页面输入数据。它没有问题。 – riachwdry23

+0

为了简单起见,只需打印出成功的json字符串即。 '{“status”:“success”}'执行查询后,并检查您的Android应用程序。 – MTahir

相关问题