2016-10-11 6 views
0

このアプリケーションを実行しようとしていますが、ログインしようとすると「logcat」にこのエラーが表示されます。メッセージ「Success Login」は、ログに記録されており、データベースに接続していることを示しますが、オブジェクトを待っている場所と文字列を受け取っている場所はわかりません。 // RestJsonServiceBEGIN_OBJECTが必要ですが、1行目のカラムがSTRINGでした1 LoginActivity

10-11 14:54:10.481 2532-7430/com.alvardev.demos.limacultural D/com.alvardev.demos.limacultural.http.RestJsonService: Respuesta data : stdClass Object ([login] => stdClass Object
(
[email] => [email protected]
[password] => 123
))
SELECT id, name, email, urlImg FROM usuario WHERE email='[email protected]' AND password='123'

Notice: Undefined index: password in C:\xampp\htdocs\culturapp\login.php on line 40

Notice: Undefined index: password in C:\xampp\htdocs\culturapp\login.php on line 40
{"success":true,"message":"Success Login","user":{"id":"26","name":"kat","email":"[email protected]","urlImg":""}}

10-11 14:54:10.541 2532-2532/com.alvardev.demos.limacultural E/LoginActivity: error: java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1

10-11 14:54:10.551 2532-2532/com.alvardev.demos.limacultural E/HttpClient: NO SERVICES

// LoginActivity

btnLogin.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 
      String user = eteUser.getText().toString(); 
      String pass = etePassword.getText().toString(); 

      if (validateLogin(user, pass)) { 
       RequestLogin login = new RequestLogin(new LoginEntity(user,pass)); 
       layLoading.setVisibility(View.VISIBLE); 
       Gson gson = new Gson(); 
       connectPost(DOMAIN + "login.php", gson.toJson(login), Const.LOGIN); 
      } 

     } 
    }); 

// BaseActionBarActivity

ます。public void connectPost(文字列のURL、文字列のJSON、int型のアクション){

Intent intentService = new Intent(this, RestJsonService.class); 


    Bundle parameters = new Bundle(); 
    parameters.putString(RestJsonService.LLAVE_CONTENIDO_PETICION,json); 
    parameters.putParcelable(RestJsonService.LLAVE_RECIBIDOR,resultReceiver); 
    parameters.putInt(RestJsonService.LLAVE_METODO_PETICION, HttpMethod.POST.getValue()); 
    parameters.putString(RestJsonService.LLAVE_URL, url); 
    parameters.putInt(RestJsonService.LLAVE_TIPO_ACCION, action); 

    intentService.putExtras(parameters); 
    startService(intentService); 
} 

p rotected無効onHandleIntent(テントの意図){

Log.d(TAG, "Iniciando servicio"); 
    String url = null; 
    String datos = null; 
    int metodo = 0; 
    int tipoAccion = 0; 
    HttpResult resultado = null; 

    Bundle parametros = intent.getExtras(); 

    url = parametros.getString(LLAVE_URL); 
    datos = parametros.getString(LLAVE_CONTENIDO_PETICION); 
    metodo = parametros.getInt(LLAVE_METODO_PETICION); 
    tipoAccion = parametros.getInt(LLAVE_TIPO_ACCION); 

    Log.d(TAG, "Llamada a la url " + url); 
    Log.d(TAG, "Llamada con datos " + datos); 
    Log.d(TAG, "Llamada metodo " + metodo); 
    Log.d(TAG, "Llamada tipoaccion " + tipoAccion); 

    ResultReceiver recibidor = parametros.getParcelable(LLAVE_RECIBIDOR); 

    HttpMethod metodoEnum = HttpMethod.forValue(metodo); 
    Log.d(TAG, "Llamada " + metodoEnum.toString()); 
    switch (metodoEnum) { 
     case POST: 
      Log.d(TAG, "antes por el post"); 

      resultado = postJson(datos, url); 


      break; 
     case GET: 
      resultado = getJson(url); 
      Log.d(TAG, "paso por el get"); 

      break; 
     default: 
      break; 
    } 
    Log.d(TAG, "Respuesta codigo : " + resultado.getStatusCode()); 

    Log.d(TAG, "Respuesta data : " + resultado.getData()); 

    Bundle datosResultado = new Bundle(); 
    datosResultado 
      .putString(LLAVE_CONTENIDO_RESPUESTA, resultado.getData()); 
    datosResultado.putInt(LLAVE_TIPO_ACCION, tipoAccion); 

    recibidor.send(resultado.getStatusCode(), datosResultado); 
    //recibidor = null; 
} 

プライベートHttpResult postJson(文字列bodyRequest、文字列のURL){

HttpPost httpPost = new HttpPost(url); 
    HttpParams httpParams = new BasicHttpParams(); 
    HttpConnectionParams.setConnectionTimeout(httpParams, 30000); 
    HttpConnectionParams.setSoTimeout(httpParams, 30000); 
    HttpClient httpClient = new DefaultHttpClient(httpParams); 

    String data = null; 
    int status = 0; 

    StringEntity se; 
    try { 
     se = new StringEntity(bodyRequest, "UTF8"); 
     se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "text/json")); 
     Log.d(TAG, "text/json"); 
     httpPost.setEntity(se); 



     HttpResponse response = httpClient.execute(httpPost); 
     status = response.getStatusLine().getStatusCode(); 

     HttpEntity entity = response.getEntity(); 
     String respuesta = EntityUtils.toString(entity); 
     data = respuesta; 

    } catch (ClientProtocolException e) { 
     status = HttpCode.ERROR.getCode(); 
     Log.e(TAG, "Error protocolo"); 
     Log.e(TAG, getErrorMessage(e, "Error al realizar la peticion")); 
    } catch (IOException e) { 
     Log.e(TAG, "Error timeout"); 
     Log.e(TAG, getErrorMessage(e, "Timeout sucedio")); 
     status = HttpCode.TIMEOUT.getCode(); 
    } catch (Exception e) { 
     Log.e(TAG, "Error general"); 

     Log.e(TAG, 
       getErrorMessage(e, 
         "Error generico sucedio al realizar la petici�n")); 
     status = HttpCode.ERROR.getCode(); 
    } 

    return new HttpResult(data, status); 
} 

答えて

1

ここでは、サーバー側のAPIの悪い実装のサンプルを持っています。 実際には、apiがjsonと応答する場合は、常にjsonと応答する必要があります。私にとっては、1つのタイムストリングと別のものを送るのは悪い習慣です - シリアライズjson。 - そう、あなたがあることを、hmac符号が正しい場合にのみチェックし、デシリアライズされます

{"success":true, "data": "some data object optional"} 
{"success":false, "error":{"errorCode":123, "errorMessage":"some error text"}, data:"some additional data object optional"} 

あなたが適切な応答hmac要求の署名を実装することができましたことを確認する:私のためとして

良い方法は以下のように、すべての応答があります確かに返答はOKで、あなたはjsonです。あなたが持っているものがわからない場合は、サーバからの文字列としてエラーテキストを受け取ることができ、Gsonでデシリアライズしようとします。とにかくあなたで

あなたはJSONに文字列の応答を解析しようとしているときにエラーが、Gsonから来ている)

0

try/catchですべてをキャッチしますが、上記の適切な方法であることができます。提供される文字列が有効なJSONではなくプレーンストリングである場合は、一般的なエラーです。エラーはおそらく、この部分から来ている:

Gson gson = new Gson(); 
connectPost(DOMAIN + "login.php", gson.toJson(login), Const.LOGIN); 

あなたはJSONにloginオブジェクトに変換しようとしている:

  RequestLogin login = new RequestLogin(new LoginEntity(user,pass)); 

を、そのオブジェクトの文字列表現をJSONではありません。 RequestLoginクラスのコードを共有していませんが、Gsonに有効なJSONを提供するようにコードを修正してください。あなたが今提供しているのは、有効なJSONではないRequestLoginクラスのtoString()出力です。

gson.toJson(login)のコンテンツを共有できますか?

関連する問題