Sebastian Sebastian - 2 years ago 116
Android Question

Json array to url in android

My json array code ...

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();

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());
Log.d("TAG_NAME", resultSet.toString());
return resultSet;

How can i sent this json array to a url to connect with a web application? should i need to convert my json array.? Or how can i do it? How can i solve it.

Answer Source

You can post entire JSON array(resultset) to PHP server using any Networking library. At PHP side, just decode that JSON array and save its value as per your need. I give you some code in which i used Retrofit 2 library:

create Interface:

public interface YOURAPI{
 Call<ResponseBody> insertSERVER(@Field("options") String jsonarray);

create another class APIClient:

private static final String BASE_URL ="";
private static Retrofit retrofit = null;

public static Retrofit getClient() {

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
    return retrofit;

In main Activity:

YOURAPI itemAPI= Apiclient.getClient().create(YOURAPI.class);
                    Call<ResponseBody> servercall=itemAPI.insertSERVER(String.valueOf(resultset));

servercall.enqueue(new Constants.BackoffCallback(retryNum) { @Override public void onResponse(Call call, Response response) { BufferedReader reader; String output = "";

                            try {
                                reader = new BufferedReader(new InputStreamReader(response.body().byteStream()));
                                output = reader.readLine();
                            } catch (IOException e) {
                        public void onFailedAfterRetry(Throwable t) {
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download