Android:如何检查是否使用齐射成功将JSON数据发送到服务器?

时间:2017-04-25 12:23:18

标签: java android json post android-volley

我想将JSON数据从我的应用程序发送到服务器。我正在使用排球库。我在这里查看了这个问题Volley send JSONObject to server with POST method。我设法从服务器获得响应,这是“正常”但我在尝试发送数据时丢失了什么或者我是否已成功将其发送到服务器?

我的数据存储在JSONObject d内,看起来像

{   "temp_mC":0,
    "humidity_ppm":28430,
    "pressure_Pa":101242,
    "temp2_mC":32937,
    "co_mV":238,
    "no2_mV":1812,
    "noise_dB":79,      
}

我打电话发布数据的方法

  public void postData(JSONObject d) {
    try {

       final String requestBody = d.toString();

        StringRequest stringRequest = new StringRequest(1, "http:.....", new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                Log.d("Response",response);
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
               Log.e("Error:",error.toString());
            }
        }) {
            @Override
            public String getBodyContentType() {
                return String.format("application/json; charset=utf-8");
            }

            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return requestBody == null ? null : requestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException uee) {
                    VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                            requestBody, "utf-8");
                    return null;
                }
            }
        };
         MySingleton.getInstance(this).addToRequestQueue(stringRequest);

    } catch (Exception e) {
        e.printStackTrace();
    }


}

MySingleton类来自示例代码

 public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;

private MySingleton(Context context) {
    mCtx = context;
    mRequestQueue = getRequestQueue();

    mImageLoader = new ImageLoader(mRequestQueue,
            new ImageLoader.ImageCache() {
                private final LruCache<String, Bitmap>
                        cache = new LruCache<String, Bitmap>(20);

                @Override
                public Bitmap getBitmap(String url) {
                    return cache.get(url);
                }

                @Override
                public void putBitmap(String url, Bitmap bitmap) {
                    cache.put(url, bitmap);
                }
            });
}

public static synchronized MySingleton getInstance(Context context) {
    if (mInstance == null) {
        mInstance = new MySingleton(context);
    }
    return mInstance;
}

public RequestQueue getRequestQueue() {
    if (mRequestQueue == null) {
        // getApplicationContext() is key, it keeps you from leaking the
        // Activity or BroadcastReceiver if someone passes one in.
        mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
    }
    return mRequestQueue;
}

public <T> void addToRequestQueue(Request<T> req) {
    getRequestQueue().add(req);
}

public ImageLoader getImageLoader() {
    return mImageLoader;
}
}

2 个答案:

答案 0 :(得分:0)

如果你从服务器得到了肯定,那么最合乎逻辑的想法是数据已被发送。当发送错误数据时,我们不知道您使用的服务器以及服务器返回客户端的内容。

当您发送错误的数据时,也许您的服务器也会发送OK状态(200),如果一切正常,您应该检查您的服务器,从凌空的响应中看起来一切都很好。

答案 1 :(得分:0)

在onResponse内成功完成请求后,如果状态代码为200则检查状态代码如下,则表示您的请求成功执行

private NetworkResponse networkResponse;
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
    networkResponse = response;
    return super.parseNetworkResponse(response);

}

 @Override
        public void onResponse(String response) {
            if(networkResponse.statusCode==200)
             {
              // then perform your task
                Log.d("Response",response);
             }
        }