【问题标题】:REST web service found response in postman but not in volley, but the status code found is 200REST Web 服务在 postman 中找到响应,但在 volley 中没有,但找到的状态码是 200
【发布时间】:2017-01-13 12:56:41
【问题描述】:

我是 Android 的初学者。我正在尝试使用 volley 在 Android 中连接休息网络服务。它在邮递员(chrome 扩展)中工作正常,但它没有得到正确的 JSON 对象响应(我找到了这样的字符串响应)

"<!doctype html... "

但是返回状态码是200。method是POST方法,body是

 { "email":"mail.xxxxxxxx@gmail.com","password":"123456" }

 Content-Type   application/json,

需要的输出

 {"success":1,"data":{"customer_id":"358","name":"xxx..","email":"mail.xxxxxx@gmail.com"},"message":"xxxxx"}

我正在尝试两种方法

1)

  try {
        RequestQueue requestQueue = Volley.newRequestQueue(MainActivity.this);
        String URL = "http://www.xxxxx";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("email", "mail.xxxxxx@gmail.com");
        jsonBody.put("password", "123456");
        final String requestBody = jsonBody.toString();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new
                Response.Listener<String() {
                    @Override
                    public void onResponse(String response) {
                        Toast.makeText(MainActivity.this, String.valueOf(response), Toast.LENGTH_SHORT).show();
                        Log.i("VOLLEY", response);
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                //Toast.makeText(MainActivity.this,String.valueOf(response),Toast.LENGTH_SHORT).show();
                Log.e("VOLLEY", error.toString());
            }
        }) {
            @Override
            public String getBodyContentType() {
                return "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;
                }
            }

            @Override
            protected Response<String parseNetworkResponse(NetworkResponse response) {
                String responseString = "";
                if (response != null) {
                    Log.i("VOLLEY RESPONSE", String.valueOf(response));
                    responseString = String.valueOf(response.statusCode);

                    // can get more details such as response.headers
                }
                return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
            }
        };

        requestQueue.add(stringRequest);
    } catch (JSONException e) {
        e.printStackTrace();
    }

        in this method i found jsonObject parsing error,

2)

  try {
        RequestQueue requestQueue =
                Volley.newRequestQueue(MainActivity.this);
        String URL = "http://www.xxxxx";
        JSONObject jsonBody = new JSONObject();
        jsonBody.put("email", "mail.xxxxx@gmail.com");
        jsonBody.put("password", "123456");
        final String requestBody = jsonBody.toString();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String() {
            @Override
            public void onResponse(String response) {
                Toast.makeText(MainActivity.this,String.valueOf(response),Toast.LENGTH_SHORT).show();
                Log.e("VOLLEY OUTPUT NEW",String.valueOf(response));**strong text**
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(MainActivity.this,String.valueOf(error),Toast.LENGTH_SHORT).show();
            }
        }){
            @Override
            public byte[] getBody() throws AuthFailureError {
                try {
                    return requestBody.getBytes("utf-8");
                } catch (UnsupportedEncodingException e) {
                    e.printStackTrace();
                }
                return null;
            }
            @Override
            public String getBodyContentType() {
                return "application/json; charset=utf-8";
            }
        };

        requestQueue.add(stringRequest);

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

在这里我找到了 200 状态码,但响应不是必需的。

【问题讨论】:

  • 你能发布你的api代码吗?
  • 因为您希望 JSON 返回,您可以使用 JsonObjectRequest 和 Response.Listener 作为 responseListener,这样可能更容易发现问题

标签: javascript android json rest


【解决方案1】:
create a class with the following 
import java.io.UnsupportedEncodingException;
import java.util.Map;    
import org.json.JSONException;
import org.json.JSONObject;    
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

public class CustomRequest extends Request<JSONObject> {

    private Listener<JSONObject> listener;
    private Map<String, String> params;

    public CustomRequest(String url, Map<String, String> params,
            Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    public CustomRequest(int method, String url, Map<String, String> params,
            Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    protected Map<String, String> getParams()
            throws com.android.volley.AuthFailureError {
        return params;
    };

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    protected void deliverResponse(JSONObject response) {
        // TODO Auto-generated method stub
        listener.onResponse(response);
    }
}

【讨论】:

  • RequestQueue requestQueue = Volley.newRequestQueue(getActivity()); CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, this.createRequestSuccessListener(), this.createRequestErrorListener()); requestQueue.add(jsObjRequest);
  • 使用上述方法
猜你喜欢
  • 2018-12-21
  • 2015-09-30
  • 1970-01-01
  • 1970-01-01
  • 2020-12-20
  • 2023-03-14
  • 2021-01-21
  • 2014-06-20
  • 1970-01-01
相关资源
最近更新 更多