简体   繁体   中英

Android Volley POST ServerError

I am trying to make a POST call on an endpoint for a pet project from my android app.

I am using volley library and I am hitting this error : com.android.volley.ServerError with message as NULL . (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" />

Also my endpoint is accessible via curl for testing :

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. See the curl request, POST data is different than what you submit from Android code. Your getJsonData() should look like below (I created a demo project and made changes, I can see the expected response):

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.

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);


        }
    });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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