预计END_OBJECT但是BEGIN_OBJECT

时间:2016-01-14 15:36:16

标签: java android android-activity

我正在尝试在我的android apk中运行登录,起初看起来很不错但是当我尝试登录或提交时,会显示一条消息

  

“预计END_OBJECT但是BEGIN_OBJECT”

我的代码:

private void AuthenticateOperator(View v) {

        try {
            Log.d(TAG,
                    "AuthenticateStudent");

            progressDialog = new ProgressDialog(MainActivity.this);
            progressDialog.setMessage("Verificando, favor espere...");
            progressDialog.setCancelable(true);
            progressDialog.show();

            Boolean hasInternet = Utils.CheckInternet(
                    getApplicationContext());

            if (hasInternet) {

                if (mUsername.getText().toString().isEmpty() ||
                        mPassword.getText().toString().isEmpty()) {

                    Utils.showToast(getApplicationContext(), "Error: empty fields");

                    return;
                }

               new ThreadedDownload().execute(Uri.parse("s"));

            } else {
                Utils.showToast(getApplicationContext(), "No hay internet");
            }

        }catch (Exception ex) {
           // Log.d(TAG ,ex.getMessage());
            //Utils.showToast(getApplicationContext(), ex.getMessage());
        }

    }

我无法弄清楚发生了什么。

ThreadedDownload 类:

class ThreadedDownload extends AsyncTask<Uri, Void, Void> {

        final Map<String,Object> params = new LinkedHashMap<>();

        @Override
        protected void onPreExecute() {

            params.put("username", mUsername.getText().toString());
            params.put("password", mPassword.getText().toString());
        }

        @Override
        protected Void doInBackground(Uri... params2) {


            if (Looper.myLooper() == null)
                Looper.prepare();

            try {
                Uri uri = new Uri.Builder()
                        .scheme(RestfulAPIService.HTTP_SCHEME)
                        .authority(RestfulAPIService.SERVER_IP)
                        .path(
                                RestfulAPIService.BASE_URL +
                                        RestfulAPIService.AUTHENTICATION_SERVICE
                        )
                        .build();

                ResponseJSONParser rp = new ResponseJSONParser();

                rp.parseJsonAuthResponse(
                        Utils.getRestApiData(uri, params));


                if(Account.getInstance().getmData()
                        .equals(RestfulAPIService.RESPONSE_FOUND) ) {

                    RestfulAPIService.username = mUsername.toString();

                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {


                                if (mRememberMe.isChecked()) {
                                    loginPrefsEditor.putBoolean("saveLogin", true);
                                    loginPrefsEditor.putString("useraccount", mUsername.getText().toString());                                    
                                    loginPrefsEditor.putString("password", mPassword.getText().toString());
                                    loginPrefsEditor.commit();

                                    Intent myIntent = new Intent(getBaseContext(), Estudio.class);
                                    startActivity(myIntent);  
                                }
                                else
                                {
                                    Intent myIntent = new Intent(getBaseContext(), Estudio.class);
                                    startActivity(myIntent);  
                                }



                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });


                }
                else
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            try {
                                Utils.showToast(getApplicationContext(), "credenciales invalidos");
                            } catch (Exception e) {
                                e.printStackTrace();
                            }
                        }
                    });

            }
            catch (final Exception hostEx){

                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Utils.showToast(getApplicationContext(), hostEx.getMessage());
                    }
                });
                hostEx.printStackTrace();
            }
            return null;


         //   return auth;
        }

        @Override
        protected void onPostExecute(Void ignore) {            

            if (progressDialog.isShowing()) {
                progressDialog.dismiss();
            }
        }   
}

JSON:

public class ResponseJSONParser {

/**
 * Used for logging purposes.
 */
@SuppressWarnings("unused")
private final String TAG =
        this.getClass().getCanonicalName();

/**
 * Constructor that initializes all the fields of interest.
 */
public ResponseJSONParser(){

}


/**
 *
 * @param reader
 * @return
 * @throws IOException
 */

// For Authentification
public void parseJsonAuthResponse(JsonReader reader)
        throws IOException {

    reader.beginObject();
    // Authentication auth = null;

    try {
        while (reader.peek() != JsonToken.END_OBJECT) {
            String name = reader.nextName();

            switch (name) {
            case Account.status_JSON:
                Log.d("TAG", "status:" + reader.nextString());
                break;

            case Account.status_message_JSON:
                Log.d("TAG", "status_message:" +  reader.nextString());
                break;

            case Account.posts_JSON:
                if (reader.peek() == JsonToken.BEGIN_ARRAY) {
                    parseAuthentication(reader);
                }
                break;

            default:
                reader.skipValue();
                break;
            }
        }
    }
    catch (Exception ex) {
        Log.d("me", ex.getMessage());
    }
    finally {
        reader.endObject();
        //  return  auth;
    }

}


// For InsertProduct

@SuppressWarnings("finally")
public  ArrayList<InsertProducts> parseJsonInsertProductResponse(JsonReader     reader)
        throws IOException {

    reader.beginObject();

    ArrayList<InsertProducts> list = new ArrayList<InsertProducts>();
    // Authentication auth = null;

    try {
        while (reader.peek() != JsonToken.END_OBJECT) {
            String name = reader.nextName();

            switch (name) {
            case Account.status_JSON:
                Log.d("TAG", "status:" + reader.nextString());
                break;

            case Account.status_message_JSON:
                Log.d("TAG", "status_message:" +  reader.nextString());
                break;

            case Account.posts_JSON:
                if (reader.peek() == JsonToken.BEGIN_ARRAY) {
                    list = (ArrayList<InsertProducts>)    parseInsertlist(reader);
                }
                break;

            default:
                reader.skipValue();
                break;
            }
        }
    }
    catch (Exception ex) {
        Log.d("me", ex.getMessage());
    }
    finally {
        reader.endObject();
        return  list;
    }

}



 /**
 *
 * @param reader
 * @return
 * @throws IOException
 */
 @SuppressWarnings({ "unused", "finally" })
 private ArrayList<InsertProducts> parseInsertlist(JsonReader reader)
        throws IOException {



    reader.beginArray();
    ArrayList<InsertProducts> list = new ArrayList();

    try {

        while (reader.peek() != JsonToken.END_ARRAY) {
            reader.beginObject();

            while(reader.hasNext()) {
                String name = reader.nextName();

                switch (name){

                case "data":
                    Account.getInstance().setmData(reader.nextString());
                    break;

                case "values":
                    reader.beginArray();

                    while(reader.peek() != JsonToken.END_ARRAY) {

                        InsertProducts tInsert = new InsertProducts();
                        reader.beginObject();

                        while(reader.hasNext()) {

                            String name2 = reader.nextName();

                            switch (name2) {

                            case InsertProducts.idProducto_JSON:
                                tInsert.setMidProducto(reader.nextString());
                                break;

                            case InsertProducts.idListaProducto_Json:
                                tInsert.setMidListaProducto(reader.nextString());
                                break;    

                            case InsertProducts.precio_Json:
                                tInsert.setMprecio(reader.nextString());
                                break;    

                                ///////////////////////

                            case InsertProducts.idUsuario_JSON:
                                tInsert.setMidUsuario(reader.nextString());
                                break;

                            case InsertProducts.latitud_Json:
                                tInsert.setMlatitud(reader.nextString());
                                break;    

                            case InsertProducts.longitud_Json:
                                tInsert.setMlongitud(reader.nextString());
                                break; 

                            case InsertProducts.isOferta_Json:
                                tInsert.setMisOferta(reader.nextString());
                                break;    

                            case InsertProducts.fechaHasta_Json:
                                tInsert.setMfechaHasta(reader.nextString());
                                break; 

                            default:
                                reader.skipValue();
                                break;
                            }
                        }
                        reader.endObject();
                        list.add(tInsert);
                    }
                    reader.endArray();
                    break;

                default:
                    reader.skipValue();
                    break;
                }
            }
            reader.endObject();
        }
        reader.endArray();

    } finally {
        return list;
    }
}




// For List of Studies

@SuppressWarnings("finally")
public  ArrayList<Study> parseJsonStudyListResponse(JsonReader reader)
        throws IOException {

    reader.beginObject();

    ArrayList<Study> list = new ArrayList<Study>();
    // Authentication auth = null;

    try {
        while (reader.peek() != JsonToken.END_OBJECT) {
            String name = reader.nextName();

            switch (name) {
            case Account.status_JSON:
                Log.d("TAG", "status:" + reader.nextString());
                break;

            case Account.status_message_JSON:
                Log.d("TAG", "status_message:" +  reader.nextString());
                break;

            case Account.posts_JSON:
                if (reader.peek() == JsonToken.BEGIN_ARRAY) {
                    list = (ArrayList<Study>) parsestudylist(reader);
                }
                break;

            default:
                reader.skipValue();
                break;
            }
        }
    }
    catch (Exception ex) {
        Log.d("me", ex.getMessage());
    }
    finally {
        reader.endObject();
        return  list;
    }

}

// For List of Product

@SuppressWarnings("finally")
public  ArrayList<ListOfProduct> parseJsonProductListResponse(JsonReader reader)
        throws IOException {

    reader.beginObject();

    ArrayList<ListOfProduct> list = new ArrayList();
    // Authentication auth = null;

    try {
        while (reader.peek() != JsonToken.END_OBJECT) {
            String name = reader.nextName();

            switch (name) {
            case Account.status_JSON:
                Log.d("TAG", "status:" + reader.nextString());
                break;

            case Account.status_message_JSON:
                Log.d("TAG", "status_message:" +  reader.nextString());
                break;

            case Account.posts_JSON:
                if (reader.peek() == JsonToken.BEGIN_ARRAY) {
                    list = (ArrayList<ListOfProduct>) parseproductlist(reader);
                }
                break;

            default:
                reader.skipValue();
                break;
            }
        }
    }
    catch (Exception ex) {
        Log.d("me", ex.getMessage());
    }
    finally {
        reader.endObject();
        return  list;
    }

}



@SuppressWarnings("finally")
public  ArrayList<Products> parseJsonProductsResponse(JsonReader reader)
        throws IOException {

    reader.beginObject();

    ArrayList<Products> list = new ArrayList();
    // Authentication auth = null;

    try {
        while (reader.peek() != JsonToken.END_OBJECT) {
            String name = reader.nextName();

            switch (name) {
            case Account.status_JSON:
                Log.d("TAG", "status:" + reader.nextString());
                break;

            case Account.status_message_JSON:
                Log.d("TAG", "status_message:" +  reader.nextString());
                break;

            case Account.posts_JSON:
                if (reader.peek() == JsonToken.BEGIN_ARRAY) {
                    list = (ArrayList<Products>) parseproduct(reader);
                }
                break;

            default:
                reader.skipValue();
                break;
            }
        }
    }
    catch (Exception ex) {
        Log.d("me", ex.getMessage());
    }
    finally {
        reader.endObject();
        return  list;
    }

}




/**
 *
 * @param reader
 * @return
 * @throws IOException
 */
// For Authentification
private void parseAuthentication(JsonReader reader)
        throws IOException {

    //  Authentication authRecord = new  Authentication();

    reader.beginArray();

    try {

        while (reader.peek() != JsonToken.END_ARRAY) {
            reader.beginObject();

            while(reader.hasNext()) {
                String name = reader.nextName();

                switch (name){

                case "data":
                    Account.getInstance().setmData(reader.nextString());
                    break;

                case "values":
                    reader.beginObject();

                    while(reader.hasNext()) {

                        String name2 = reader.nextName();

                        switch (name2) {

                        case Account.auth_JSON:
                              Account.getInstance().setmAuth(reader.nextString());
                            break;

                        case Account.email_JSON:
                            Account.getInstance().setmAuth(reader.nextString());
                            break;

                        case Account.password_JSON:
                            Account.getInstance().setmAuth(reader.nextString());
                            break;

                        case Account.telefono_JSON:
                            Account.getInstance().setmTelefono(reader.nextString());
                            break;


                        case Account.nombre_JSON:
                            Account.getInstance().setmNombre(reader.nextString());
                            break;

                        case Account.apellido_JSON:
                            Account.getInstance().setmApellido(reader.nextString());
                            break;

                    /*  case Account.imagen_JSON:
                            Account.getInstance().setmImagen(Utils.downloadImage(reader.nextString()));
                            break;
*/
                        case Account.tEstudio_JSON:
                            Account.getInstance().setmTotalEstudio(reader.nextString());
                            break;

                        default:
                            reader.skipValue();
                            break;
                        }
                    }
                    break;

                default:
                    reader.skipValue();
                    break;
                }
            }
            reader.endObject();
        }
        reader.endArray();

    } finally {
        // return authRecord;
    }
}


/**
 *
 * @param reader
 * @return
 * @throws IOException
 */
// For Products
@SuppressWarnings({ "unused", "finally" })
private ArrayList<Products> parseproduct(JsonReader reader)
        throws IOException {

    //  Authentication authRecord = new  Authentication();

    reader.beginArray();
    ArrayList<Products> list = new ArrayList();

    try {

        while (reader.peek() != JsonToken.END_ARRAY) {
            reader.beginObject();

            while(reader.hasNext()) {
                String name = reader.nextName();

                switch (name){

                case "data":
                    Account.getInstance().setmData(reader.nextString());
                    break;

                case "values":
                    reader.beginArray();

                    while(reader.peek() != JsonToken.END_ARRAY) {

                        Products tProduct = new Products();
                        reader.beginObject();

                        while(reader.hasNext()) {

                            String name2 = reader.nextName();

                            switch (name2) {

                            case Products.idProducto_JSON:
                                tProduct.setMidProducto(reader.nextString());
                                break;

                            case Products.producto_Json:
                                tProduct.setMproducto(reader.nextString());
                                break;  

                            case Products.categoria_Json:
                                tProduct.setMcategoria(reader.nextString());
                                break;

                            case Products.tipo_JSON:
                                tProduct.setMtipo(reader.nextString());
                                break;   


                            case Products.unidad_Json:
                                tProduct.setMunidad(reader.nextString());
                                break;   



                            default:
                                reader.skipValue();
                                break;
                            }
                        }
                        reader.endObject();
                        list.add(tProduct);
                    }
                    reader.endArray();
                    break;

                default:
                    reader.skipValue();
                    break;
                }
            }
            reader.endObject();
        }
        reader.endArray();

    } finally {
        return list;
    }
}






/**
 *
 * @param reader
 * @return
 * @throws IOException
 */
// For List of Product
@SuppressWarnings({ "unused", "finally" })
private ArrayList<ListOfProduct> parseproductlist(JsonReader reader)
        throws IOException {

    //  Authentication authRecord = new  Authentication();

    reader.beginArray();
    ArrayList<ListOfProduct> list = new ArrayList();

    try {

        while (reader.peek() != JsonToken.END_ARRAY) {
            reader.beginObject();

            while(reader.hasNext()) {
                String name = reader.nextName();

                switch (name){

                case "data":
                    Account.getInstance().setmData(reader.nextString());
                    break;

                case "values":
                    reader.beginArray();

                    while(reader.peek() != JsonToken.END_ARRAY) {

                        ListOfProduct tlistProduct = new ListOfProduct();
                        reader.beginObject();

                        while(reader.hasNext()) {

                            String name2 = reader.nextName();

                            switch (name2) {

                            case ListOfProduct.idListaProducto_JSON:
                                tlistProduct.setMidListProducto(reader.nextString());
                                break;

                            case ListOfProduct.ListaProducto_Json:
                                tlistProduct.setMnombreListaProducto(reader.nextString());
                                break;    



                            default:
                                reader.skipValue();
                                break;
                            }
                        }
                        reader.endObject();
                        list.add(tlistProduct);
                    }
                    reader.endArray();
                    break;

                default:
                    reader.skipValue();
                    break;
                }
            }
            reader.endObject();
        }
        reader.endArray();

    } finally {
        return list;
    }
}





/**
 *
 * @param reader
 * @return
 * @throws IOException
 */
@SuppressWarnings({ "unused", "finally" })
private ArrayList<Study> parsestudylist(JsonReader reader)
        throws IOException {

    //  Authentication authRecord = new  Authentication();

    reader.beginArray();
    ArrayList<Study> list = new ArrayList();

    try {

        while (reader.peek() != JsonToken.END_ARRAY) {
            reader.beginObject();

            while(reader.hasNext()) {
                String name = reader.nextName();

                switch (name){

                case "data":
                    Account.getInstance().setmData(reader.nextString());
                    break;

                case "values":
                    reader.beginArray();

                    while(reader.peek() != JsonToken.END_ARRAY) {

                        Study tStudy = new Study();
                        reader.beginObject();

                        while(reader.hasNext()) {

                            String name2 = reader.nextName();

                            switch (name2) {

                            case Study.studylist_JSON:
                                tStudy.setMstudylist(reader.nextString());
                                break;

                            case Study.Sucursal_Json:
                                tStudy.setMnombreSucursal(reader.nextString());
                                break;    

                            case Study.idEstudioSucursal_Json:
                                tStudy.setMidEstudioSucursal(reader.nextString());
                                break;    

                            default:
                                reader.skipValue();
                                break;
                            }
                        }
                        reader.endObject();
                        list.add(tStudy);
                    }
                    reader.endArray();
                    break;

                default:
                    reader.skipValue();
                    break;
                }
            }
            reader.endObject();
        }
        reader.endArray();

    } finally {
        return list;
    }
}

}

0 个答案:

没有答案