我的项目在注册时使用JSON对象作为响应。
这是注册类:
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 { &#34; id_user_pemilik_kos&#34;:74 类型为org.json.JSONObject的类型无法转换为JSONArray
我不知道问题是什么,为解决这个问题可以做些什么?
答案 0 :(得分:1)
看起来生活在请求的URL的服务返回单个JSON对象,而不是JSON对象数组。
尝试将strReq
从JsonArrayRequest
更改为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之间的区别。
答案 1 :(得分:0)
更改代码如下:
JsonObjectRequest strReq = new JsonObjectRequest(url,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}
你已经给出了你将从响应中获得JsonArray。在onResponse()
方法体中,您使用了JsonObject。你搞砸了。