简体   繁体   English

Android Volley POST服务器错误

[英]Android Volley POST ServerError

I am trying to make a POST call on an endpoint for a pet project from my android app. 我正在尝试通过我的android应用在宠物项目的端点上进行POST调用。

I am using volley library and I am hitting this error : com.android.volley.ServerError with message as NULL . 我正在使用volley库,并且遇到以下错误: com.android.volley.ServerErrormessageNULL (I am having a hard time trying to figure out what am I doing wrong) (我很难找出我在做什么错)

I have ensured I have given internet connection option in my manifest : <uses-permission android:name="android.permission.INTERNET" /> 我确保清单中已提供Internet连接选项: <uses-permission android:name="android.permission.INTERNET" />

Also my endpoint is accessible via curl for testing : 我的端点也可以通过curl访问以进行测试:

curl -X POST -H "Content-Type: application/json" -H "Cache-Control: no-cache" -H "Postman-Token: 122eedf5-8ca5-c1a6-06d9-5cc1986d2b23" -d '{
    "departure_time" : "2016-07-21 07:00:00",
    "arrival_time" : "2016-07-21 08:30:00",
    "location_flow": ["Milpitas+ca", "SanCarlos+Ca"],
    "buffer_time": [0, 30, 0],
    "mode": "driving"
}' "https://sudtrafficalarm.herokuapp.com/v1/wakecheck"

output : 输出:

{
  "estimated_arrival_time": "2016-07-21 08:05:01",
  "journey_duration": 2101,
  "wake_status": false
}

This is my main activity : 这是我的主要活动:

package com.sudhishkr.sudtrafficalarmandroid;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final RequestQueue queue = Volley.newRequestQueue(this);
        Button button = (Button) findViewById(R.id.button);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                //Toast.makeText(MainActivity.this, SystemTime.date2String(SystemTime.getCurrentTime()), Toast.LENGTH_SHORT).show();
                final String REGISTER_URL = "https://sudtrafficalarm.herokuapp.com/v1/wakecheck";
                JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, REGISTER_URL, getJsonData(),
                        new Response.Listener<JSONObject>() {
                            @Override
                            public void onResponse(JSONObject response) {
                                Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();
                            }
                        },
                        new Response.ErrorListener() {
                            @Override
                            public void onErrorResponse(VolleyError error) {
                                Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();
                            }
                        }
                );
                queue.add(jsonRequest);


            }
        });
    }

    public JSONObject getJsonData() {
        JSONObject params = new JSONObject();
        try {
            params.put("departure_time", "2016-07-21 07:00:00");
            params.put("arrival_time", "2016-07-21 08:30:00");
            params.put("mode", "driving");
            JSONArray address = new JSONArray();
            address.put("Milpitas+ca");
            address.put("SanCarlos+Ca");
            params.put("location_flow", address);
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return params;
    }


}

Debugging the code - takes me here. 调试代码-带我到这里。 And I have no message or information as to why this occurs. 而且我没有任何消息或信息说明为什么会发生这种情况。

在此处输入图片说明

You have not added 'buffer_time' parameter in your Android code. 您尚未在Android代码中添加“ buffer_time”参数。 See the curl request, POST data is different than what you submit from Android code. 查看curl请求,POST数据与您从Android代码提交的数据不同。 Your getJsonData() should look like below (I created a demo project and made changes, I can see the expected response): 您的getJsonData()应该如下所示(我创建了一个演示项目并进行了更改,可以看到预期的响应):

public JSONObject getJsonData() {
    JSONObject params = new JSONObject();
    try {
        params.put("departure_time", "2016-07-21 07:00:00");
        params.put("arrival_time", "2016-07-21 08:30:00");
        params.put("mode", "driving");
        JSONArray address = new JSONArray();
        address.put("Milpitas+ca");
        address.put("SanCarlos+Ca");
        params.put("location_flow", address);

        JSONArray buffer_time = new JSONArray();
        buffer_time.put(0);
        buffer_time.put(30);
        buffer_time.put(0);
        params.put("buffer_time", buffer_time);
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return params;
}

I guess it's happening because you are trying to print toast in background thread which suppose to gets called in Main Thread. 我猜是因为您正在尝试在后台线程中打印吐司,而该线程可能在主线程中被调用。 The possible reason could be the callback methods of the api will be called in background thread because the network operations always happen in the background thread. 可能的原因可能是api的回调方法将在后台线程中被调用,因为网络操作始终在后台线程中发生。

Use this updated code 使用此更新的代码

button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            //Toast.makeText(MainActivity.this, SystemTime.date2String(SystemTime.getCurrentTime()), Toast.LENGTH_SHORT).show();
            final String REGISTER_URL = "https://sudtrafficalarm.herokuapp.com/v1/wakecheck";
            JsonObjectRequest jsonRequest = new JsonObjectRequest(Request.Method.POST, REGISTER_URL, getJsonData(),
                    new Response.Listener<JSONObject>() {
                        @Override
                        public void onResponse(JSONObject response) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, response.toString(), Toast.LENGTH_SHORT).show();        
                                }
                            });

                        }
                    },
                    new Response.ErrorListener() {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    Toast.makeText(MainActivity.this, error.toString(), Toast.LENGTH_SHORT).show();        
                                }
                            });

                        }
                    }
            );
            queue.add(jsonRequest);


        }
    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM