0

I have to create a json arrray from the sqlite db and sent that json array as json fromat to a server My code for converting sqlite to json is...

  public JSONArray getResults()
{

    String myPath = "/data/data/com.example.sebastian.patientdetails/databases/MyDBName.db";// Set path to your database

    String myTable = TABLE_NAME;//Set name of your table



    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("Data", cursor.getString(i) );
                        rowObject.put(cursor.getColumnName(i) ,  cursor.getString(i) );
                    }
                    else
                    {
                        rowObject.put( cursor.getColumnName(i) ,  "" );
                    }
                }
                catch( Exception e )
                {
                    Log.d("Data", e.getMessage()  );
                }
            }
        }
        resultSet.put(rowObject);
        cursor.moveToNext();
    }
    cursor.close();
   Log.d("Data", resultSet.toString() );

    return resultSet;

}

How can i send this json array to server to hit as json format at the server side? How can i do it. Please help me...

  • @Bhavnik ive to hit json formatted file at the server... How can i do it –  Nov 03 '16 at 07:33
  • If you wanna upload file with JsonArray you should go with Multipart. To upload file to server from device multipart is used, So in first place create a text file, write data in text file, find path of that file and then upload that file to server..... But I'll suggest you to pass that JsonArray as string, that is very easy way to pass data to server side. – Bhavnik Nov 03 '16 at 09:13
  • Refer this link, if it could help you:- http://stackoverflow.com/questions/14351870/send-txt-file-document-file-to-the-server-in-android – Bhavnik Nov 03 '16 at 09:15

2 Answers2

0

You can use JsonArray request with POST method using the Google Volley library

sanket pahuja
  • 325
  • 2
  • 12
0

You can also use OkHttp to send post data to server. Add compile 'com.squareup.okhttp3:okhttp:3.4.1' in your build.gradle in dependencies. And use below code

public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}

Full code here

Call post method with URL and your JSON to send on server.

Community
  • 1
  • 1
Bills
  • 768
  • 7
  • 19