相关文章推荐
拉风的勺子  ·  ERROR sqoop.Sqoop: ...·  7 月前    · 
拉风的勺子  ·  Speech Recongnition ...·  10 月前    · 
拉风的勺子  ·  VB.Net - Forms | ...·  10 月前    · 
拉风的勺子  ·  Xilinx Customer Community·  11 月前    · 
拉风的勺子  ·  minecraft forge教程 / ...·  11 月前    · 
拉风的勺子  ·  Kubernetes ingress ...·  1 年前    · 
拉风的勺子  ·  java ...·  1 年前    · 
聪明伶俐的课本  ·  SpringBoot ...·  1小时前    · 
爱看球的牙膏  ·  IDEA ...·  1小时前    · 
帅气的红茶  ·  清华大学出版社·  3 小时前    · 
private void add_scouting
            (final String pest,final String chem,final String remarks) {
        // Tag used to cancel the request
        String tag_string_collect = "Add_scouting";
        pDialog.setMessage("Add Scouting ...");
        showDialog();
        StringRequest strReq = new StringRequest(Request.Method.POST,
                AppConfig.URL_ADDPLANTING, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d(TAG, "Scouting Response: " + response.toString());
                hideDialog();
                try {
                    JSONObject jObj = new JSONObject(response);
                    boolean error = jObj.getBoolean("error");
                    if (!error) {
                        Toast.makeText(planting.this, "Scouting Added successfully.", Toast.LENGTH_LONG).show();
                    } else {
                        // Error occurred in registration. Get the error
                        // message
                        String errorMsg = jObj.getString("error_msg");
                        Toast.makeText(planting.this,
                                errorMsg, Toast.LENGTH_LONG).show();
                } catch (JSONException e) {
                    e.printStackTrace();
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e(TAG, "AddPlanting Error: " + error.getMessage());
//                Toast.makeText(getContext(),
//                        error.getMessage(), Toast.LENGTH_LONG).show();
                hideDialog();
            @Override
            protected Map<String, String> getParams() {
                // Posting params to register url
                Map<String, String> params = new HashMap<String, String>();
                params.put("bcode",pest);
                return params;
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(strReq, tag_string_collect);
    private void showDialog() {
        if (!pDialog.isShowing())
            pDialog.show();
    private void hideDialog() {
        if (pDialog.isShowing())
            pDialog.dismiss();
    
2 个评论
你好,欢迎来到StackOverflow。看起来你的问题的标题与你的代码和截图没有关系。你能说得更精确些吗?
你目前在密钥pest下发送bcode,你的服务器端有什么问题?
java
android
json
api
Bored Ape
Bored Ape
发布于 2022-09-06
1 个回答
Şageldi Alyýew
Şageldi Alyýew
发布于 2022-09-08
已采纳
0 人赞同

1) 在应用程序的build.gradle中实现这一依赖关系

implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:converter-gson:2.1.0'

2) 创建APIClient.java类,把这段代码放到该类中。

import java.util.concurrent.TimeUnit;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class APIClient {
    private static Retrofit retrofit = null;
    public static OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .readTimeout(180, TimeUnit.SECONDS)
            .writeTimeout(180,TimeUnit.SECONDS)
            .connectTimeout(180, TimeUnit.SECONDS)
            .build();
    public static Retrofit getClient() {
        return new Retrofit.Builder()
                .baseUrl("http://BASE_URL_HERE/")
                .addConverterFactory(GsonConverterFactory.create())
                .client(okHttpClient)
                .build();

BASE URL是你的服务器主机名。例如,你有apis。所有的apis都以http://localhost:8080/api/.这将是BASE URL。因为所有的api都以这个网址开始。

3) 创建ApiInterface.java接口文件,并在该文件中插入这段代码。

import java.util.ArrayList;
import retrofit2.Call;
import retrofit2.http.Body;
import retrofit2.http.GET;
import retrofit2.http.Header;
import retrofit2.http.POST;
import retrofit2.http.PUT;
import retrofit2.http.Path;
import retrofit2.http.Query;
public interface ApiInterface {
    @POST("insert-multiple-selection")
    Call<YOUR_RESPONSE_CLASS> acceptOrder(@Body ArrayList<String> selections);

4) 在你的按钮onclick方法中加入这段代码。

ApiInterface apiInterface= APIClient.getClient().create(ApiInterface.class);
        ArrayList<String> selections=new ArrayList<>();
        // To add value
        selections.add("YOUR_VALUE_1");
        selections.add("YOUR_VALUE_2");
        selections.add("YOUR_VALUE_3");
        // To remove value
        selections.remove("YOUR_VALUE_1");
        Call<YOUR_RESPONSE_CLASS> call=apiInterface.sendSelections(selections);
        call.enqueue(new Callback<YOUR_RESPONSE_CLASS>() {
            @Override
            public void onResponse(Call<YOUR_RESPONSE_CLASS> call, Response<YOUR_RESPONSE_CLASS> response) {
               if(response.isSuccessful()){
                   Toast.makeText(context, "Successfully sent!", Toast.LENGTH_SHORT).show();
                } else {
                   Toast.makeText(context, "Error code: "+response.code(), Toast.LENGTH_SHORT).show();
            @Override
 
推荐文章