2016-04-22 138 views
0

我想通过Web服务访问Android应用程序。 在Web服务中执行新的注册。 在android应用程序中,进行新注册的xml文件。 数据已成功保存在SQL服务器数据库中,并通过Web服务正确保存并返回jason字符串中的数据。Android,字符串不能转换为JSONObject

但当字符串转换为JSONObject的,它给我的错误是这样的:

org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject 

LoginActivity

package com.example.shy.wel.activity; 

import android.app.Activity; 
import android.app.ProgressDialog; 
import android.content.Intent; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

import com.android.volley.Request.Method; 
import com.android.volley.Response; 
import com.android.volley.VolleyError; 
import com.android.volley.toolbox.StringRequest; 
import com.example.shy.wel.LoadingActivity; 
import com.example.shy.wel.MainActivity; 
import com.example.shy.wel.R; 
import com.example.shy.wel.app.AppConfig; 
import com.example.shy.wel.app.AppController; 
import com.example.shy.wel.helper.SQLiteHandler; 
import com.example.shy.wel.helper.SessionManager; 

import org.json.JSONException; 
import org.json.JSONObject; 

import java.util.HashMap; 
import java.util.Map; 

public class LoginActivity extends Activity { 
    private static final String TAG = RegisterActivity.class.getSimpleName(); 
    private Button btnLogin; 
    private Button btnLinkToRegister; 
    private EditText inputEmail; 
    private EditText inputPassword; 
    private ProgressDialog pDialog; 
    private SessionManager session; 
    private SQLiteHandler db; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_login); 

     Intent loading = new Intent(this, LoadingActivity.class); 
     startActivity(loading); 

     inputEmail = (EditText) findViewById(R.id.email); 
     inputPassword = (EditText) findViewById(R.id.password); 
     btnLogin = (Button) findViewById(R.id.btnLogin); 
     btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen); 

     // Progress dialog 
     pDialog = new ProgressDialog(this); 
     pDialog.setCancelable(false); 

     // SQLite database handler 
     db = new SQLiteHandler(getApplicationContext()); 

     // Session manager 
     session = new SessionManager(getApplicationContext()); 

     // Check if user is already logged in or not 
     if (session.isLoggedIn()) { 
      // User is already logged in. Take him to main activity 
      Intent intent = new Intent(LoginActivity.this, MainActivity.class); 
      startActivity(intent); 
      finish(); 
     } 

     // Login button Click Event 
     btnLogin.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       String email = inputEmail.getText().toString().trim(); 
       String password = inputPassword.getText().toString().trim(); 

       // Check for empty data in the form 
       if (!email.isEmpty() && !password.isEmpty()) { 
        // login user 
        checkLogin(email, password); 
       } else { 
        // Prompt user to enter credentials 
        Toast.makeText(getApplicationContext(), 
          "Please enter the credentials!", Toast.LENGTH_LONG) 
          .show(); 
       } 
      } 

     }); 

     // Link to Register Screen 
     btnLinkToRegister.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View view) { 
       Intent i = new Intent(getApplicationContext(), 
         RegisterActivity.class); 
       startActivity(i); 
       finish(); 
      } 
     }); 

    } 

    /** 
    * function to verify login details in mysql db 
    * */ 
    private void checkLogin(final String email, final String password) { 
     // Tag used to cancel the request 
     String tag_string_req = "req_login"; 

     pDialog.setMessage("Logging in ..."); 
     showDialog(); 

     StringRequest strReq = new StringRequest(Method.POST, 
       AppConfig.URL_LOGIN, new Response.Listener<String>() { 


      @Override 
      public void onResponse(String response) { 
       Log.d(TAG, "Login Response: " + response.toString()); 
       hideDialog(); 

       try { 
        JSONObject jObj = new JSONObject(response); 
        boolean error = jObj.getBoolean("error"); 

        // Check for error node in json 
        if (!error) { 
         // user successfully logged in 
         // Create login session 
         session.setLogin(true); 

         // Now store the user in SQLite 
         String uid = jObj.getString("uid"); 

         JSONObject user = jObj.getJSONObject("user"); 
         String name = user.getString("name"); 
         String email = user.getString("email"); 
         String created_at = user.getString("created_at"); 

         // Inserting row in users table 
         db.addUser(name, email, uid, created_at); 

         // Launch main activity 
         Intent intent = new Intent(LoginActivity.this, 
           MainActivity.class); 
         startActivity(intent); 
         finish(); 
        } else { 
         // Error in login. Get the error message 
         String errorMsg = jObj.getString("error_msg"); 
         Toast.makeText(getApplicationContext(), 
           errorMsg, Toast.LENGTH_LONG).show(); 
        } 
       } catch (JSONException e) { 
        // JSON error 
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
       } 

      } 
     }, new Response.ErrorListener() { 

      @Override 
      public void onErrorResponse(VolleyError error) { 
       Log.e(TAG, "Login Error: " + error.getMessage()); 
       Toast.makeText(getApplicationContext(), 
         error.getMessage(), Toast.LENGTH_LONG).show(); 
       hideDialog(); 
      } 
     }) { 

      @Override 
      protected Map<String, String> getParams() { 
       // Posting parameters to login url 
       Map<String, String> params = new HashMap<String, String>(); 
       params.put("email", email); 
       params.put("password", password); 

       return params; 
      } 

     }; 

     // Adding request to request queue 
     AppController.getInstance().addToRequestQueue(strReq, tag_string_req); 
    } 

    private void showDialog() { 
     if (!pDialog.isShowing()) 
      pDialog.show(); 
    } 

    private void hideDialog() { 
     if (pDialog.isShowing()) 
      pDialog.dismiss(); 
    } 
} 

我logcat的

04-22 13:41:55.092 14473-14521/com.example.shy.wel E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7facc84b6ad0 
04-22 13:41:55.117 14473-14473/com.example.shy.wel W/System.err: org.json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObject 
04-22 13:41:55.117 14473-14473/com.example.shy.wel W/System.err:  at org.json.JSON.typeMismatch(JSON.java:111) 
04-22 13:41:55.117 14473-14473/com.example.shy.wel W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:160) 
04-22 13:41:55.118 14473-14473/com.example.shy.wel W/System.err:  at org.json.JSONObject.<init>(JSONObject.java:173) 
04-22 13:41:55.118 14473-14473/com.example.shy.wel W/System.err:  at com.example.shy.wel.activity.LoginActivity$3.onResponse(LoginActivity.java:129) 
04-22 13:41:55.118 14473-14473/com.example.shy.wel W/System.err:  at com.example.shy.wel.activity.LoginActivity$3.onResponse(LoginActivity.java:120) 
04-22 13:41:55.118 14473-14473/com.example.shy.wel W/System.err:  at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) 
04-22 13:41:55.118 14473-14473/com.example.shy.wel W/System.err:  at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) 
04-22 13:41:55.118 14473-14473/com.example.shy.wel W/System.err:  at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) 
04-22 13:41:55.119 14473-14473/com.example.shy.wel W/System.err:  at android.os.Handler.handleCallback(Handler.java:739) 
04-22 13:41:55.119 14473-14473/com.example.shy.wel W/System.err:  at android.os.Handler.dispatchMessage(Handler.java:95) 
04-22 13:41:55.119 14473-14473/com.example.shy.wel W/System.err:  at android.os.Looper.loop(Looper.java:148) 
04-22 13:41:55.119 14473-14473/com.example.shy.wel W/System.err:  at android.app.ActivityThread.main(ActivityThread.java:5417) 
04-22 13:41:55.119 14473-14473/com.example.shy.wel W/System.err:  at java.lang.reflect.Method.invoke(Native Method) 
04-22 13:41:55.119 14473-14473/com.example.shy.wel W/System.err:  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
04-22 13:41:55.119 14473-14473/com.example.shy.wel W/System.err:  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
04-22 13:41:55.123 1606-1779/system_process W/InputMethodManagerService: Window already focused, ignoring focus gain of: [email protected] attribute=null, token = [email protected] 
04-22 13:41:55.174 14473-14521/com.example.shy.wel W/EGL_emulation: eglSurfaceAttrib not implemented 
04-22 13:41:55.174 14473-14521/com.example.shy.wel W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x7facbe4a2500, error=EGL_SUCCESS 
04-22 13:41:58.629 14473-14521/com.example.shy.wel E/Surface: getSlotFromBufferLocked: unknown buffer: 0x7facc84b6c90 
+0

由Web服务返回的数据是无效的,在这里尝试http://jsonlint.com/验证它 – user3802077

+0

你返回的字符串是' NoChinDeluxe

回答

0

代码似乎F国家统计局。问题可能出现在JSON中。尝试在此页面上生成的JSON JSON Formatter and Validator

祝你好运!

0

是一个常见的问题,如果你检查JSONObject的构造说:

Parameters: 
source - `A string beginning with { (left brace) and ending with } (right brace).` 

这意味着可能缺少括号...尝试做:

JSONObject jObj = new JSONObject("{" + response + "}"); 

还是可以的相反,它可以在字符串周围有一些丢失的字符(调试和检查),在这种情况下,解决方案将是:

new JSONObject(response.substring(response.indexOf("{"), response.lastIndexOf("}") + 1)); 

祝你好运=)