2017-05-26 59 views
-1

我有我的項目註冊到Web服務與JSON對象的響應。Erorrvoley org.json.JSONException:類型org.json.JSONObject的值不能轉換爲JSONArray

這是註冊類:

public class RegisterReq extends AppCompatActivity { 

ProgressDialog pDialogreg; 
Button register; 
EditText tx_username,tx_password, 
     tx_nama,tx_alamat, 
     tx_telepon,tx_email, 
     tx_no_ktp, tx_confirm_pass; 
Intent intent; 
int success; 
ConnectivityManager conMgr; 
private SessionManager session; 

private static final String TAG = RegisterReq.class.getSimpleName(); 
private static final String TAG_SUCCESS = "success"; 
private static final String TAG_MESSAGE = "massage"; 
String tag_json_obj = "json_obj_req"; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_register_req); 
    pDialogreg = new ProgressDialog(this); 
    pDialogreg.setCancelable(false); 
    register = (Button) findViewById(R.id.btn_register); 
    tx_username = (EditText) findViewById(R.id.username); 
    tx_password = (EditText)findViewById(R.id.password); 
    tx_confirm_pass = (EditText) findViewById(R.id.confirm_password); 
    tx_nama = (EditText) findViewById(R.id.nama); 
    tx_alamat = (EditText) findViewById(R.id.alamat); 
    tx_email= (EditText) findViewById(R.id.email); 
    tx_telepon=(EditText) findViewById(R.id.telepon); 
    tx_no_ktp = (EditText) findViewById(R.id.no_ktp); 

    register.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      String username = tx_username.getText().toString().trim(); 
      String password = tx_password.getText().toString().trim(); 
      String confirm_password = tx_confirm_pass.getText().toString().trim(); 
      String nama = tx_nama.getText().toString().trim(); 
      String alamat =tx_alamat.getText().toString().trim(); 
      String telepon = tx_telepon.getText().toString().trim(); 
      String email = tx_email.getText().toString().trim(); 
      String no_ktp = tx_no_ktp.getText().toString().trim(); 

      if(password.equals(confirm_password)){ 
       checkRegister(username, password, nama, alamat, telepon, email, no_ktp); 
      } else { 
       Toast.makeText(getApplicationContext(), "Password not Match with Confirm", Toast.LENGTH_SHORT).show(); 
      } 
     } 
    }); 

    session = new SessionManager(getApplicationContext()); 
    if (session.isLoggedIn()) { 
     // User is already logged in. Take him to main activity 
     Intent intent = new Intent(RegisterReq.this, 
       MainActivity.class); 
     startActivity(intent); 
     finish(); 
    } 

} 

private void checkRegister(final String username, 
          final String password, 
          final String nama, 
          final String alamat, 
          final String telepon, 
          final String email, 
          final String no_ktp 
) { 

    pDialogreg.setMessage("Register ..."); 
    showDialog(); 
    String url ="http://gis.sigjalan.com/web-services-db.php?flag=fAddUser&username="+username+ 
      "&password="+password+ 
      "&nama="+nama+ 
      "&alamat="+alamat+ 
      "&telepon="+telepon+ 
      "&email="+email+ 
      "&no_ktp="+no_ktp; 
    JsonArrayRequest strReq = new JsonArrayRequest(url, 
      new Response.Listener<JSONArray>() { 

     @Override 
     public void onResponse(JSONArray response) { 
      Log.e(TAG, "Register Response: " + response.toString()); 
      hideDialog(); 
      String result = response.toString(); 
       try { 
        JSONObject jsonObject = new JSONObject(result); 
        JSONArray Jarray = jsonObject.getJSONArray("contacts"); 
        for (int i = 0; i < result.length(); i++) { 
         JSONObject Jasonobject = Jarray.getJSONObject(i); 
        } 
        Toast.makeText(getApplicationContext(),"User successfully registered. Try login now!", Toast.LENGTH_LONG).show(); 
        startActivity(new Intent(RegisterReq.this,MainActivity.class)); 

       } catch (JSONException e){ 
        e.printStackTrace(); 
        Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show(); 
       } 
      } 


    }, 
      //untuk error 
      new Response.ErrorListener() { 
       @Override 
       public void onErrorResponse(VolleyError error) { 
        Log.e(TAG, "Erorrvoley " + error.getMessage()); 
        Toast.makeText(getApplicationContext(), 
          "error voley"+error.getMessage(), Toast.LENGTH_LONG).show(); 

       } 
      }); 

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

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

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

這是登記表格的活動文件:

<?xml version="1.0" encoding="utf-8"?> 
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.gun21.gunawan.gispro.RegisterReq"> 
    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent"> 
    android:orientation="vertical" 
    <ScrollView android:id="@+id/register_req_form" android:layout_height="wrap_content" android:layout_width="match_parent"> 

     <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:orientation="vertical"> 

     <View android:id="@+id/View1" android:layout_width="fill_parent" android:layout_height="1dp" android:layout_gravity="center" android:background="#448AFF" /> 

     <TextView android:id="@+id/TextView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="30dip" android:textColor="#C0392B" android:text="REGISTER BELOW" android:textSize="16dip" 
     /> 
     <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> 
      <EditText android:id="@+id/username" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:hint="Username" android:inputType="textEmailAddress" android:singleLine="true" 
      /> 
     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> 
      <EditText android:id="@+id/password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:hint="Password" android:password="true" android:singleLine="true" /> 
     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> 
      <EditText android:id="@+id/confirm_password" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_margin="10dp" android:hint="Comfirmation Password" android:password="true" /> 
     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> 
      <EditText android:id="@+id/nama" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:hint="Nama" /> 
     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> 
      <EditText android:id="@+id/alamat" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:hint="Alamat" /> 
     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> 
      <EditText android:id="@+id/telepon" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:hint="Telepon" /> 
     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> 
      <EditText android:id="@+id/email" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:hint="Email" /> 
     </android.support.design.widget.TextInputLayout> 

     <android.support.design.widget.TextInputLayout android:layout_width="match_parent" android:layout_height="wrap_content"> 
      <EditText android:id="@+id/no_ktp" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:hint="No KTP" /> 
     </android.support.design.widget.TextInputLayout> 

     <Button android:id="@+id/btn_register" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:layout_gravity="center" android:layout_marginBottom="15dp" android:background="#2980B9" android:textColor="#fff" 
      android:text="Register" android:textStyle="bold" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" /> 

     </LinearLayout> 
    </ScrollView> 
    </LinearLayout> 

</android.support.constraint.ConstraintLayout> 

這是響應:

{ 
    "id_user_pemilik_kos": 73 
} 

,這是錯誤:

05 - 26 03: 04: 49.513 30080 - 30080/com.gun21.gunawan.gispro E/RegisterReq: Erorrvoley org.json.JSONException: Value { "id_user_pemilik_kos": 74 } of type org.json.JSONObject cannot be converted to JSONArray

我不知道是什麼問題所以,可以做些什麼來解決這個問題?

+0

錯誤消息讓您知道「JSONObject無法轉換爲JSONArray」。請檢查您的數據回覆 您能分享完整的數據回覆嗎? –

回答

1

看起來生活在請求的URL的服務正在返回一個JSON對象,而不是一個JSON對象數組。

嘗試將strReqJsonArrayRequest更改爲JsonObjectRequest。這意味着onResponse函數將接受JSONObject參數而不是JSONArray

JsonObjectRequest strReq = new JsonObjectRequest(url, 
     new Response.Listener<JSONObject>() { 

    @Override 
    public void onResponse(JSONObject response) { 
     // ... 
     // Note: you will likely not need this conversion to a JSONObject anymore 
     // JSONObject jsonObject = new JSONObject(result); <-- Not needed now 
    } 
} 

相信this page將有助於解釋JsonArrayRequest和JsonObjectRequest之間的差異。

0

改變這樣的代碼:

JsonObjectRequest strReq = new JsonObjectRequest(url, 
    new Response.Listener<JSONObject>() { 

    @Override 
    public void onResponse(JSONObject response) { 
    } 
} 

你給了,你會從響應得到JsonArray。在onResponse()方法的主體中使用了JsonObject。你混淆了。

相關問題