Reputation: 583
I have values of the states and cities of India stored in JSON. That code will be to big to put up in here so I tried the same thing but with only states. But that file is not getting compiled.
Error Message :
2020-08-14 19:06:02.754 10347-10347/com.luv.foodforhunger W/System.err: org.json.JSONException: Value [{
"State Name": "Andaman & Nicobar Islands"
}, {
"State Name": "Andhra Pradesh"
}, {
"State Name": "Arunachal Pradesh"
}, {
"State Name": "Assam"
}, {
"State Name": "Bihar"
}, {
"State Name": "Chandigarh"
}, {
"State Name": "Chattisgarh"
}, {
"State Name": "Dadra and Nagar Haveli and Daman and Diu"
}, {
"State Name": "Delhi"
}, {
"State Name": "Goa"
}, {
"State Name": "Gujarat"
}, {
"State Name": "Haryana"
}, {
"State Name": "Himachal Pradesh"
}, {
"State Name": "Jammu & Kashmir"
}, {
"State Name": "Jharkhand"
}, {
"State Name": "Karnataka"
}, {
"State Name": "Kerala"
}, {
"State Name": "Lakshadweep"
}, {
"State Name": "Madhya Pradesh"
}, {
"State Name": "Maharashtra"
}, {
"State Name": "Manipur"
}, {
"State Name": "Meghalaya"
}, {
"State Name": "Mizoram"
}, {
"State Name": "Nagaland"
}, {
"State Name": "Odisha"
}, {
"State Name": "Pondicherry"
}, {
"State Name": "Punjab"
}, {
"State Name": "Rajasthan"
}, {
"State Name": "Sikkim"
}, {
"State Name": "Tamil Nadu"
}, {
"State Name": "Telangana"
}, {
"State Name": "Tripura"
}, {
"State Name": "Uttar Pradesh"
}, {
"State Name": "Uttarakhand"
}, {
"State Name": "West Bengal"
}
]at 0 of type org.json.JSONArray cannot be converted to JSONObject
JSON Code :
[[{
"State Name": "Andaman & Nicobar Islands"
}, {
"State Name": "Andhra Pradesh"
}, {
"State Name": "Arunachal Pradesh"
}, {
"State Name": "Assam"
}, {
"State Name": "Bihar"
}, {
"State Name": "Chandigarh"
}, {
"State Name": "Chattisgarh"
}, {
"State Name": "Dadra and Nagar Haveli and Daman and Diu"
}, {
"State Name": "Delhi"
}, {
"State Name": "Goa"
}, {
"State Name": "Gujarat"
}, {
"State Name": "Haryana"
}, {
"State Name": "Himachal Pradesh"
}, {
"State Name": "Jammu & Kashmir"
}, {
"State Name": "Jharkhand"
}, {
"State Name": "Karnataka"
}, {
"State Name": "Kerala"
}, {
"State Name": "Lakshadweep"
}, {
"State Name": "Madhya Pradesh"
}, {
"State Name": "Maharashtra"
}, {
"State Name": "Manipur"
}, {
"State Name": "Meghalaya"
}, {
"State Name": "Mizoram"
}, {
"State Name": "Nagaland"
}, {
"State Name": "Odisha"
}, {
"State Name": "Pondicherry"
}, {
"State Name": "Punjab"
}, {
"State Name": "Rajasthan"
}, {
"State Name": "Sikkim"
}, {
"State Name": "Tamil Nadu"
}, {
"State Name": "Telangana"
}, {
"State Name": "Tripura"
}, {
"State Name": "Uttar Pradesh"
}, {
"State Name": "Uttarakhand"
}, {
"State Name": "West Bengal"
}
]]
DonorAdressSetUpActivity.java
public class DonorAdressSetUpActivity extends AppCompatActivity {
EditText flatNoEditText, buildingNameEditText, streetAndSectorEditText, cityEditText, pinCodeEditText;
Spinner stateTV;
String url = "http://luvbarnwal.in/states_of_india.html";
ArrayList<String> states;
private void loadSpinnerData(String url) {
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for (int i = 0; i<jsonArray.length(); i++){
JSONObject jsonObject = jsonArray.getJSONObject(i);
String state = jsonObject.getString("State Name");
states.add(state);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(DonorAdressSetUpActivity.this, android.R.layout.simple_spinner_item, states);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
stateTV.setAdapter(arrayAdapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
int socketTimeout = 30000;
RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
stringRequest.setRetryPolicy(policy);
requestQueue.add(stringRequest);
}
public void continuee(View view){
if (flatNoEditText.getText().toString().isEmpty()){
flatNoEditText.setHint("Enter Your Flat Number");
} else if (buildingNameEditText.getText().toString().isEmpty()){
buildingNameEditText.setHint("Enter Your Building Name");
} else if (streetAndSectorEditText.getText().toString().isEmpty()){
streetAndSectorEditText.setHint("Enter Your Street & Sector Name");
} else if (cityEditText.getText().toString().isEmpty()){
cityEditText.setHint("Enter Your City");
} else if (pinCodeEditText.getText().toString().isEmpty()){
pinCodeEditText.setHint("Enter Your Pin Code");
} else {
ParseQuery<ParseObject> query = ParseQuery.getQuery("DonorDetails");
query.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
query.findInBackground(new FindCallback<ParseObject>() {
@Override
public void done(List<ParseObject> objects, ParseException e) {
for (ParseObject object : objects) {
object.put("flatNo", flatNoEditText.getText().toString());
object.put("buildingName", buildingNameEditText.getText().toString());
object.put("streetAndSectorName", streetAndSectorEditText.getText().toString());
object.put("cityName", cityEditText.getText().toString());
object.put("pinCode", pinCodeEditText.getText().toString());
object.put("stateName", stateTV.getSelectedItem().toString());
object.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null) {
ParseQuery<ParseUser> parseQuery = ParseUser.getQuery();
parseQuery.whereEqualTo("username", ParseUser.getCurrentUser().getUsername());
parseQuery.findInBackground(new FindCallback<ParseUser>() {
@Override
public void done(List<ParseUser> objects, ParseException e) {
if (e == null){
if (objects.size() > 0){
for (ParseUser user : objects){
user.put("stateName", stateTV.getSelectedItem().toString());
user.saveInBackground(new SaveCallback() {
@Override
public void done(ParseException e) {
if (e == null){
Log.i("Successful", "Your address details has been stored");
Intent intent = new Intent(getApplicationContext(), DonorContactSetUpActivity.class);
startActivity(intent);
}
}
});
}
}
} else {
String message = e.getMessage();
if (Objects.requireNonNull(message).toLowerCase().contains("java")) {
message = e.getMessage().substring(e.getMessage().indexOf(" "));
}
Toast.makeText(DonorAdressSetUpActivity.this, message, Toast.LENGTH_SHORT).show();
}
}
});
} else {
String message = e.getMessage();
if (Objects.requireNonNull(message).toLowerCase().contains("java")) {
message = e.getMessage().substring(e.getMessage().indexOf(" "));
}
Toast.makeText(DonorAdressSetUpActivity.this, message, Toast.LENGTH_SHORT).show();
}
}
});
}
}
});
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_donor_adress_set_up);
setTitle("Enter Your Address");
flatNoEditText = (EditText) findViewById(R.id.flatNoEditText);
buildingNameEditText = (EditText) findViewById(R.id.buildingNameEditText);
streetAndSectorEditText = (EditText) findViewById(R.id.streetAndSectorEditText);
cityEditText = (EditText) findViewById(R.id.cityEditText);
pinCodeEditText = (EditText) findViewById(R.id.pinCodeEditText);
stateTV = (Spinner) findViewById(R.id.stateTV);
states = new ArrayList<>();
loadSpinnerData(url);
stateTV.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String state = stateTV.getItemAtPosition(stateTV.getSelectedItemPosition()).toString();
Toast.makeText(DonorAdressSetUpActivity.this, state, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}
}
activity_donor_adress_set_up.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".DonorAdressSetUpActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="508dp"
android:layout_marginEnd="12dp"
android:text="*"
android:textColor="@android:color/holo_red_dark"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView2" />
<EditText
android:id="@+id/pinCodeEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="Pin Code"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/cityEditText" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="414dp"
android:layout_marginEnd="12dp"
android:text="*"
android:textColor="@android:color/holo_red_dark"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView2" />
<EditText
android:id="@+id/cityEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="City"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/streetAndSectorEditText" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="324dp"
android:layout_marginEnd="12dp"
android:text="*"
android:textColor="@android:color/holo_red_dark"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView2" />
<EditText
android:id="@+id/streetAndSectorEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="Street and Sector"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/buildingNameEditText" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="230dp"
android:layout_marginEnd="12dp"
android:text="*"
android:textColor="@android:color/holo_red_dark"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView2" />
<TextView
android:id="@+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:gravity="center_horizontal"
android:text="Enter your Address"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/flatNoEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="Flat No."
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView2" />
<EditText
android:id="@+id/buildingNameEditText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="48dp"
android:ems="10"
android:hint="Building Name"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/flatNoEditText" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="69dp"
android:onClick="continuee"
android:text="Continue"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/stateTV" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:layout_marginEnd="15dp"
android:text="*"
android:textColor="@android:color/holo_red_dark"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView2" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="140dp"
android:layout_marginEnd="12dp"
android:text="*"
android:textColor="@android:color/holo_red_dark"
android:textSize="15sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/textView2" />
<Spinner
android:id="@+id/stateTV"
android:layout_width="399dp"
android:layout_height="34dp"
android:layout_marginTop="48dp"
android:prompt="@string/state_name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/pinCodeEditText" />
</android.support.constraint.ConstraintLayout>
</ScrollView>
</LinearLayout>
Please help me resolve my problem, because this app is really important to make.
Upvotes: 0
Views: 76
Reputation: 1294
jsonArray
is pointing towards incorrect reference. If you check your raw JSON, it's containing an array inside an array.
[ // -> Outer JSONArray
[ // -> Inner JSONArray
{ // -> JSONObject
"State Name":"Andaman & Nicobar Islands"
},....
]
]
Replace this:
JSONArray jsonArray = new JSONArray(response);
With:
JSONArray outerJSONArray = new JSONArray(response);
JSONArray jsonArray = outerJSONArray.getJSONArray(0)
Upvotes: 3