2016-08-31 4 views
0

これはJSONObjectを要求するコードです。 String要求を処理している間、完全に動作しましたが、JSONObjectが必要です。出力API出力をチェックするにはボレーを使用してJSONObjectRequestのJSONobjectを受信できません

Map<String, String> params = new HashMap<String, String>(); 
    params.put("username", username); 
    params.put("password", password); 
    params.put("request", "login"); 

    JSONObject jsonObj = new JSONObject(params); 

    String url="http://example.com/android/login.php"; 
    JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, url,jsonObj, 
      new Response.Listener<JSONObject>() 
      { 


       @Override 
       public void onResponse(JSONObject response) { 
        // response 
        // JSONObject json = JSONObject.toJson(response); 
        // Toast.makeText(Login.this, "Your ID is ."+response, Toast.LENGTH_LONG).show(); 

        //Log.d("Response", response.toString()); 

        Intent r = new Intent(Login.this,Cus_home.class); 
        startActivity(r); 

       } 
      }, 
      new Response.ErrorListener() 
      { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        // error 
        Log.d("Error.Response", error.toString()); 
       } 
      } 
    ); 

    mRequestQueue.add(postRequest); 

です: [{"id":"77","type":"1"}]

しかし、Androidのコードを実行した後、私はこの

Error.Response: com.android.volley.ParseError: org.json.JSONException: End of input at character 1 of 

を取得する任意のヘルプのヘルプは本当にあなたは

+0

次のようになりますが、あなたが見たことがあり、{ "firstNameの": "ジョン"、 "lastNameの" "ドウ"} http://stackoverflow.com/questions/23220695/send-post-request with json-data-using-volley?この構文は次のように使用することをお勧めします: 'final JSONObject jsonBody = new JSONObject(" {\ "type \":\ "example \"} ");' –

+0

JsonObjectの受信に問題があります。それが私を助けることができるかどうかわからない –

+0

@SimonPhoenix JsonObjectは私の入力フィールドからです。もっと何かを提案してください –

答えて

0

を高く評価しています無効なJSONオブジェクトを戻しています。オブジェクトの代わりにJSON Stringを返しています。

JSONオブジェクトは中括弧で囲まれています。例えば `

あなたonResonse方法は、この

public void onResponse(JSONObject response) { 
        Log.d("response", response.optString("id") + " " + 
          response.optString("type")); 

} 
+0

はい私はそれを理解しました。私はOutを{"id": "77"、 "type": "1"}としていますが、それでもResponse.ErrorListener()で同じエラーが発生します。 –

+0

私はそれをテストしました。すべてがうまくいきます。 – saty

関連する問題