2017-12-17 17 views
0

私は生徒を投稿した後、自分のIDを返します。このIDをJSONから解析して後で使用するために変数に格納したいと思っています。応答が道による「」ここ は私のコードであるparse json to android in parseNetwork <string>

try { 
          RequestQueue requestQueue = Volley.newRequestQueue(Register.this); 
          String URL = "http://192.168.0.148/WebApplication7/api/users"; 
          JSONObject jsonBody = new JSONObject(); 
          jsonBody.put("tblRegisteredUsers_UserName", userName.getText().toString().trim()); 
          jsonBody.put("tblRegisteredUsers_FirstName", firstName.getText().toString()); 
          jsonBody.put("tblRegisteredUsers_SecondName", secondName.getText().toString()); 
          jsonBody.put("tblRegisteredUsers_LastName", familyName.getText().toString()); 
          jsonBody.put("tblRegisteredUsers_Country", nationality.toString()); 
          jsonBody.put("tblRegisteredUsers_MobileNumber", mobileNo.getText().toString()); 
          jsonBody.put("tblRegisteredUsers_Email", email.getText().toString()); 
          jsonBody.put("tblRegisteredUsers_Nationality", nationality.toString()); 
          jsonBody.put("tblRegisteredUsers_Active", "false"); 
          jsonBody.put("tblRegisteredUsers_PayType", saved_payment_type.toString()); 
          jsonBody.put("tblRegisteredUsers_DraftInfo", saved_payment_type.toString()); 
          jsonBody.put("tblRegisteredUsers_AccountStates", "pending"); 
          jsonBody.put("tblRegisteredUsers_Password", encrypt(password.getText().toString())); 
          jsonBody.put("tblRegisteredUsers_registrationDate", DateFormat.getDateTimeInstance().format(new Date()).toString()); 
          jsonBody.put("tblRegisteredUsers_nickName", userName.getText().toString().trim()); 
          jsonBody.put("tblRegisteredUsers_Activeby_FKID", "-1"); 
          jsonBody.put("tblRegisteredUsers_Year", "1"); 
          jsonBody.put("tblRegisteredUsers_ActivebyPayment", "false"); 
          jsonBody.put("tblRegisteredUsers_intensified", contract_Type.toString()); 


          final String requestBody = jsonBody.toString(); 

          StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() { 
           @Override 
           public void onResponse(String response) { 

             if (response.equals("success")) { 
              //login authenticated. Start the next activity of your app 
              Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show(); 
              // Toast.makeText(getApplicationContext(), "registered successfully ", Toast.LENGTH_SHORT).show(); 


             } else { 
              //login failed. prompt to re-enter the credentials 
              Toast.makeText(Register.this, "Failed to log In", Toast.LENGTH_SHORT).show(); 

              // Log.i("VOLLEY", response); 
              Toast.makeText(getApplicationContext(), response.toString(), Toast.LENGTH_LONG).show(); 
             } 
            } 
          }, new Response.ErrorListener() { 
           @Override 
           public void onErrorResponse(VolleyError error) { 
            Log.e("VOLLEY", error.toString()); 
           } 
          }) { 
           @Override 
           public String getBodyContentType() { 
            return "application/json; charset=utf-8"; 
           } 

           @Override 
           public byte[] getBody() throws AuthFailureError { 
            try { 
             return requestBody == null ? null : requestBody.getBytes("utf-8"); 
            } catch (UnsupportedEncodingException uee) { 
             VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8"); 
             return null; 
            } 
           } 

           @Override 
           protected Response<String> parseNetworkResponse(NetworkResponse response) { 
            try { 

    /** 
    First you will have to convert the NetworkResponse into a jsonstring. 
    Then that json string can be converted into the required java object 
    using gson 
    **/ 
             addEncodeing2Request(response); 
             return 
super.parseNetworkResponse(response); 
            } catch (UnsupportedEncodingException e) { 
             e.printStackTrace(); 
             return Response.error(new ParseError(e)); 
            } 
           } 
          }; 

          requestQueue.add(stringRequest); 
         } catch (JSONException e) { 
          e.printStackTrace(); 
         } catch (Exception e) { 
          e.printStackTrace(); 
         } 


        } else { 
         Toast.makeText(getApplicationContext(), "الرجاء قيول لائحة النظم", Toast.LENGTH_SHORT).show(); 
        } 
       } else { 
        Toast.makeText(getApplicationContext(), "password does not match", Toast.LENGTH_SHORT).show(); 
       } 
      } else { 
       Toast.makeText(getApplicationContext(), "password is not strong", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 



} 

せずにユーザーIDを示すこのフォーム1234年になると、解析ネットワーク方式でのreturn文は、コンパイラエラー は、ここでは応答

     { 
        "tblRegisteredUsers_UserPKID": 3034, 
         "tblRegisteredUsers_UserName": "hi21323", 
          "tblRegisteredUsers_Password": null, 
"tblRegisteredUsers_FirstName": null, 
"tblRegisteredUsers_SecondName": null, 
"tblRegisteredUsers_LastName": null, 
"tblRegisteredUsers_Country": null, 
"tblRegisteredUsers_City": null, 
"tblRegisteredUsers_Gender": null, 
"tblRegisteredUsers_BirthDate": null, 
"tblRegisteredUsers_Education": null, 
"tblRegisteredUsers_Job": null, 
"tblRegisteredUsers_HomePhone": null, 
"tblRegisteredUsers_MobileNumber": null, 
"tblRegisteredUsers_FaxNumber": null, 
"tblRegisteredUsers_Email": null, 
"tblRegisteredUsers_HowYouKnowUS": null, 
"tblRegisteredUsers_Nationality": null, 
"tblRegisteredUsers_Active": null, 
"tblRegisteredUsers_PayType": null, 
"tblRegisteredUsers_photo": null, 
"tblRegisteredUsers_DraftInfo": null, 
"tblRegisteredUsers_AccountStates": null, 
"tblRegisteredUsers_registrationDate": null, 
"tblRegisteredUsers_nickName": null, 
"tblRegisteredUsers_Activeby_FKID": null, 
"tblRegisteredUsers_ActivationDate": null, 
"tblRegisteredUsers_Year": null, 
"tblRegisteredUsers_ActivebyPayment": null, 
"tblRegisteredUsers_intensified": null, 
"tblRegisteredUsers_Shiping_Address": null, 
"tblRegisteredUsers_intensified_Status": null, 
"tblRegisteredUsers_ReactivationDateTime": null, 
"tblRegisteredUsers_ActivebyPayment_Intensive": null, 
"tblRegisteredUsers_ActivationDate_IntensiveChange": null, 
"tblRegisteredUsers_Installment": null, 
"tblRegisteredUsers_InstallmentActivationDate": null, 
"tblRegisteredUsers_Installment_ActivePayment": null, 
"tblRegisteredUsers_Hide": null, 
"tblRegisteredUsers_Hide_User_FK_ID": null, 
"tblRegisteredUsers_Hide_DateTime": null, 
"tblRegisteredUsers_Renew_Datetime": null 
ありました

}

を任意のヘルプはあなたのJSONの特定のオブジェクト/構造を持っている場合は、あなたがどこかにMyStructureObject.classを実装し、それにすべてのこれらの変数を追加したい場合があります

+0

あなたのコードに何らかの形でifsよりもelses – donfuxx

+0

JSONを共有してください。このリンクを試すhttps://stackoverflow.com/questions/9605913/how-to-parse-json-in-android – R2R

+0

私のqestionhttpsを更新しました://stackoverflow.com/questions/9605913/how-to-parse-json - アンドロイド –

答えて

0

をいただければ幸いです。

MyStructureObject{ 
String id; 
    public String getIdField(){ 
     return id; 
    } 
... // add all other variables of your json structure 
} 

あなたはcom.google.code.gson(from maven gradle repository)のようなJSONライブラリを使用して、あなたのJsonStringからJsonObjectをシリアライズ(またはデシリアライズ)できるより。私はこれは少し:) を役に立てば幸い

MySturctureObject instance = new Gson().fromJson(yourJsonString,MyStructureObject.class); 
String id = instance.getIdField(); 

私はすでに成功し、私自身のAndroidのJava/libgdxアプリケーションでgsonライブラリを追加しましたが、多分あなたは代わりに別のライブラリを使用します。 これは単なる提案です。