发送JSON到服务器齐射

时间:2020-04-06 10:36:56

标签: android json post android-volley

美好的一天,

我需要一些建议,我不知道如何正确地做到这一点

我想使用齐射向我的服务器发送JSON,

几天来,我一直在为这种简单的事情而苦苦挣扎,但我的大脑却变得糊涂了,

我要发送的JSON称为“测试”

我尝试将其作为字符串发送,但文本对于URL而言太多

我尝试过Google等也无济于事,将不胜感激任何帮助

排球发布代码

int x = 0;
int p = 1;
while (x < Array.size() && p < Array.size()) {
    StaffMemebers = "Staff Member " + p++ + " : " + Array.get(x++);
    Test.add(StaffMemebers);
    Uri.Builder PostURLBuilder = new Uri.Builder();

    PostURLBuilder.scheme("https")
        .authority("www.EXAMPLE.co.za")
        .appendPath("app-createproject-test.asp")
        .appendQueryParameter("MyForm", "Yes")
        .appendQueryParameter("ClientID", clientId)
        .appendQueryParameter("Username", email)
        .appendQueryParameter("Pwd", pwd)
        .appendQueryParameter("TimeLogDueDate", dateText.getText().toString())
        .appendQueryParameter("TimeLogStartDate", startdate.getText().toString())
        .appendQueryParameter("ProjectManager", ManagerSelected)
        .appendQueryParameter("AllocatedClient", ClientSelected)
        .appendQueryParameter("Description", Description.getText().toString())
        .appendQueryParameter("SelectedStaff", String.valueOf(Test))
        .appendQueryParameter("Notes", notesEditText.getText().toString())
        .appendQueryParameter("LogType", "p");
    postURL = PostURLBuilder.build().toString();
}

Toasty.info(this, postURL, Toasty.LENGTH_LONG).show();

final ProgressDialog pd = new ProgressDialog(createProject.this);

pd.setMessage("Please Wait....");

pd.setCanceledOnTouchOutside(false);

pd.show();

RequestQueue queue = Volley.newRequestQueue(this);

StringRequest postRequest = new StringRequest(Request.Method.POST, postURL, new Response.Listener < String > () {

        @SuppressLint("SetTextI18n")
        @Override
        public void onResponse(String response) {

            pd.cancel();

            if (response.contains("Time Logged Successfully !")) {

                AddStaff.setText("Project Created");

                AddStaff.setBackgroundColor(getResources().getColor(R.color.Green));

                AddStaff.setTextColor(getResources().getColor(R.color.Black));

                Toasty.success(createProject.this, response, Toast.LENGTH_LONG).show();
            } else {

                AddStaff.setText("Error Creating Project");

                AddStaff.setBackgroundColor(getResources().getColor(R.color.Red));

                AddStaff.setTextColor(getResources().getColor(R.color.Black));

                Toasty.error(createProject.this, "Error Logging time, Check your Internet Connection", Toast.LENGTH_LONG).show();
            }
        }
    }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {

            pd.cancel();

            Toasty.error(createProject.this, "Time Log Failed", Toast.LENGTH_LONG).show();
        }
    }
);

int socketTimeout = 30000;

RetryPolicy policy = new DefaultRetryPolicy(socketTimeout, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
postRequest.setRetryPolicy(policy);
queue.add(postRequest);
}

0 个答案:

没有答案
相关问题