如何从Android向基于ASP.NET C的web服务器发送JSON字符串?

如何从Android向基于ASP.NET C的web服务器发送JSON字符串?,android,json,http-post,Android,Json,Http Post,我无法从android发布或发送基于ASP.NETC的web服务器上的JSON字符串 这是我的代码,我使用游标从SQLite数据库检索数据,并将这些数据转换为JSONobject,然后转换为JSONstring 代码如下: protected Boolean doInBackground(final String... args) { try { JSONObject parrent = new JSONObject(); // JSONObject

我无法从android发布或发送基于ASP.NETC的web服务器上的JSON字符串

这是我的代码,我使用游标从SQLite数据库检索数据,并将这些数据转换为JSONobject,然后转换为JSONstring

代码如下:

protected Boolean doInBackground(final String... args) {

    try {

        JSONObject parrent = new JSONObject();
        // JSONObject jMainObject = new JSONObject();
        JSONArray jArray = new JSONArray();

        Cursor Online = MainActivity.mydb.rawQuery("select * from myTable", null);

        while (Online.moveToNext()) {

            JSONObject jObject = new JSONObject();
            jObject.put("CategoryType", Online.getString(0));
            jObject.put("CategoryID", Online.getString(1));
            jObject.put("CategoryName", Online.getString(2));
            jObject.put("CustomerId", Online.getString(3));
            jObject.put("CustomerName", Online.getString(4));
            jObject.put("Accountno", Online.getString(5));
            jObject.put("Balance", Online.getString(6));
            jObject.put("Installment", Online.getString(7));
            jObject.put("Amount", Online.getString(8));
            jObject.put("Collected", Online.getString(9));
            jObject.put("Dueinstnum", Online.getString(10));

            jObject.put("customer_id", Online.getString(11));
            jObject.put("dueInstNum", Online.getString(12));

            jObject.put("account_id", Online.getString(13));
            jObject.put("branch_id", Online.getString(14));
            jObject.put("customer_id", Online.getString(15));
            jObject.put("id", Online.getString(16));

            jArray.put(jObject);

            // String JSONString = jObject.toString();
        }

        parrent.put("FildCollections", jArray);
        parrent.put("ProgramOrganizerId","70cff4d5-cc0f-4bf8-80de-23dd82d90719");
        parrent.put("BranchId", "bde14105-4617-4d07-9ab8-a95e98f8c5a5");
        parrent.put("Password", "Pass@123");
        parrent.put("UserId", "5103");
但现在在下一行之后,我想把这个JSONstring发送到C web服务器

代码:

我从子菜单调用了我的函数,如下所示:

调用函数代码:

case R.id.submenu1_1:


if (item.isChecked())
    item.setChecked(false);
else {
    item.setChecked(true);

    // Export Online

    try {
        new ExportDatabaseOnline().execute();
    } catch (Exception ex) {
        Log.e("Error in ActivityB", ex.toString());
    }

}
// Toast.makeText(this, "Clicked: Menu No. 2 - SubMenu No .1",
// Toast.LENGTH_SHORT).show();
return true;
现在是完整的函数代码,我在前面的部分中已经告诉过你。因此,我的完整功能代码是:

public class ExportDatabaseOnline extends AsyncTask<String, Void, Boolean> {
    private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);

    // private HttpResponse response;

    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Please wait...");
        this.dialog.setTitle("Exporting Database To Online");
        this.dialog.show();

    }

    protected Boolean doInBackground(final String... args) {

        try {

            JSONObject parrent = new JSONObject();
            // JSONObject jMainObject = new JSONObject();
            JSONArray jArray = new JSONArray();

            Cursor Online = MainActivity.mydb.rawQuery("select * from myTable", null);

            while (Online.moveToNext()) {

                JSONObject jObject = new JSONObject();
                jObject.put("CategoryType", Online.getString(0));
                jObject.put("CategoryID", Online.getString(1));
                jObject.put("CategoryName", Online.getString(2));
                jObject.put("CustomerId", Online.getString(3));
                jObject.put("CustomerName", Online.getString(4));
                jObject.put("Accountno", Online.getString(5));
                jObject.put("Balance", Online.getString(6));
                jObject.put("Installment", Online.getString(7));
                jObject.put("Amount", Online.getString(8));
                jObject.put("Collected", Online.getString(9));
                jObject.put("Dueinstnum", Online.getString(10));

                jObject.put("customer_id", Online.getString(11));
                jObject.put("dueInstNum", Online.getString(12));

                jObject.put("account_id", Online.getString(13));
                jObject.put("branch_id", Online.getString(14));
                jObject.put("customer_id", Online.getString(15));
                jObject.put("id", Online.getString(16));

                jArray.put(jObject);

                // String JSONString = jObject.toString();
            }

            parrent.put("FildCollections", jArray);
            parrent.put("ProgramOrganizerId","70cff4d5-cc0f-4bf8-80de-23dd82d90719");
            parrent.put("BranchId", "bde14105-4617-4d07-9ab8-a95e98f8c5a5");
            parrent.put("Password", "Pass@123");
            parrent.put("UserId", "5103");

            HttpPost httppost = new HttpPost("Posting url");

            StringEntity entity = new StringEntity(parrent.toString(),"UTF-8");
            entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
            entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
            httppost.addHeader("language", "en");
            httppost.addHeader("Content-Type", "application/json");
            httppost.addHeader("user_Id", "5103");
            httppost.addHeader("user_Pass", "Pass@123");
            httppost.setEntity(entity);
            // Send request to WCF service
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpResponse response = httpClient.execute(httppost);

            return true;

        } catch (Exception e) {
            Log.e("ActivityB", e.getMessage(), e);
            return false;
        }
    }

    protected void onPostExecute(final Boolean success) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
        if (success) {
            Toast.makeText(MainActivity.this, "Export successful!",
                Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "Export failed",
                Toast.LENGTH_SHORT).show();
        }
    }
}
此代码不起作用,它显示成功消息,但不会向网站发布任何JSON字符串

public class ExportDatabaseOnline extends AsyncTask<String, Void, Boolean> {
    private final ProgressDialog dialog = new ProgressDialog(MainActivity.this);

    // private HttpResponse response;

    @Override
    protected void onPreExecute() {
        this.dialog.setMessage("Please wait...");
        this.dialog.setTitle("Exporting Database To Online");
        this.dialog.show();

    }

    protected Boolean doInBackground(final String... args) {

        try {

            JSONObject parrent = new JSONObject();
            // JSONObject jMainObject = new JSONObject();
            JSONArray jArray = new JSONArray();

            Cursor Online = MainActivity.mydb.rawQuery("select * from myTable", null);

            while (Online.moveToNext()) {

                JSONObject jObject = new JSONObject();
                jObject.put("CategoryType", Online.getString(0));
                jObject.put("CategoryID", Online.getString(1));
                jObject.put("CategoryName", Online.getString(2));
                jObject.put("CustomerId", Online.getString(3));
                jObject.put("CustomerName", Online.getString(4));
                jObject.put("Accountno", Online.getString(5));
                jObject.put("Balance", Online.getString(6));
                jObject.put("Installment", Online.getString(7));
                jObject.put("Amount", Online.getString(8));
                jObject.put("Collected", Online.getString(9));
                jObject.put("Dueinstnum", Online.getString(10));

                jObject.put("customer_id", Online.getString(11));
                jObject.put("dueInstNum", Online.getString(12));

                jObject.put("account_id", Online.getString(13));
                jObject.put("branch_id", Online.getString(14));
                jObject.put("customer_id", Online.getString(15));
                jObject.put("id", Online.getString(16));

                jArray.put(jObject);

                // String JSONString = jObject.toString();
            }

            parrent.put("FildCollections", jArray);
            parrent.put("ProgramOrganizerId","70cff4d5-cc0f-4bf8-80de-23dd82d90719");
            parrent.put("BranchId", "bde14105-4617-4d07-9ab8-a95e98f8c5a5");
            parrent.put("Password", "Pass@123");
            parrent.put("UserId", "5103");

            HttpPost httppost = new HttpPost("Posting url");

            StringEntity entity = new StringEntity(parrent.toString(),"UTF-8");
            entity.setContentType("application/json;charset=UTF-8");//text/plain;charset=UTF-8
            entity.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,"application/json;charset=UTF-8"));
            httppost.addHeader("language", "en");
            httppost.addHeader("Content-Type", "application/json");
            httppost.addHeader("user_Id", "5103");
            httppost.addHeader("user_Pass", "Pass@123");
            httppost.setEntity(entity);
            // Send request to WCF service
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpResponse response = httpClient.execute(httppost);

            return true;

        } catch (Exception e) {
            Log.e("ActivityB", e.getMessage(), e);
            return false;
        }
    }

    protected void onPostExecute(final Boolean success) {
        if (this.dialog.isShowing()) {
            this.dialog.dismiss();
        }
        if (success) {
            Toast.makeText(MainActivity.this, "Export successful!",
                Toast.LENGTH_SHORT).show();
        } else {
            Toast.makeText(MainActivity.this, "Export failed",
                Toast.LENGTH_SHORT).show();
        }
    }
}