Sebastian Sebastian - 1 month ago 10
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();

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;
}


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

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{
 @FormUrlEncoded
 @POST("add_to_db.php")
 Call<ResponseBody> insertSERVER(@Field("options") String jsonarray);
}

create another class APIClient:

private static final String BASE_URL ="http://yoururl.com/";
private static Retrofit retrofit = null;

public static Retrofit getClient() {

    if (retrofit == null) {
        retrofit = new Retrofit.Builder()
                .baseUrl(BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
    }
    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();
                                Log.e("TAG",""+output);
                            } catch (IOException e) {
                                e.printStackTrace();
                            }
                        @Override
                        public void onFailedAfterRetry(Throwable t) {
                        }
                    });
Comments