Sebastian Sebastian - 1 month ago 15
JSON Question

Json to server error

I got this error...

10-27 12:07:08.101 10065-10108/com.example.sebastian.patientdetails D/TAG_NAME: [{"_id":"1","name":"Sss","dob":"10\/1\/2000","gender":"Male","mobile":"1234567890","address":"asdasd"}]
10-27 12:07:08.102 10065-10108/com.example.sebastian.patientdetails E/Volley: [141] NetworkDispatcher.run: Unhandled exception java.lang.ClassCastException: org.json.JSONArray cannot be cast to java.util.Map
java.lang.ClassCastException: org.json.JSONArray cannot be cast to java.util.Map
at com.example.sebastian.patientdetails.MainActivity$3$3.getParams(MainActivity.java:112)
at com.android.volley.Request.getBody(Request.java:460)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:253)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:227)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:97)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114)


I need to sent my json array to a server url. When i click the button i got this error. Can anyody suggest me whats the error means.? Can anybody suggest me a good tutorial for json to server inserting.?

MainActivity Code...

...
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {

Intent i = new Intent(getApplicationContext(), AddPatient.class);
startActivity(i);
}
});

b2 = (Button) findViewById(R.id.button3);
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {

getResults();

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

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}){

protected Map<String,String>getParams()throws AuthFailureError {
Map<String,String> parameters= new HashMap<String, String>();
return (Map<String, String>) getResults();
}
};
requestQueue.add(request);
}
});
}

private JSONArray getResults() {

String myPath = "/data/data/com.example.sebastian.patientdetails/databases/" + "MyDBName.db";

String myTable = "patients";


SQLiteDatabase myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);

String searchQuery = "SELECT * FROM " + myTable;
Cursor cursor = myDataBase.rawQuery(searchQuery, null);

JSONArray resultSet = new JSONArray();

cursor.moveToFirst();
while (!cursor.isAfterLast()) {

int totalColumn = cursor.getColumnCount();
JSONObject rowObject = new JSONObject();

for (int i = 0; i < totalColumn; i++) {
if (cursor.getColumnName(i) != null) {
try {
if (cursor.getString(i) != null) {
Log.d("TAG_NAME", cursor.getString(i));
rowObject.put(cursor.getColumnName(i), cursor.getString(i));
} else {
rowObject.put(cursor.getColumnName(i), "");
}
} catch (Exception e) {
Log.d("TAG_NAME", e.getMessage());
}
}
}
resultSet.put(rowObject);
cursor.moveToNext();
}
cursor.close();
Log.d("TAG_NAME", resultSet.toString());
return resultSet;
}

Answer

You cannot convert JSONArray to Map , but you can convert JSONObject to Map and can get value by keys. First get JSONObject from the JSONArray and convert it to Map.

Comments