繁体   English   中英

REST Web服务在邮递员中找到响应,但在齐射中未找到响应,但是找到的状态码为200

[英]REST web service found response in postman but not in volley, but the status code found is 200

我是Android的初学者。 我尝试使用凌空连接Android中的其余Web服务。 它在邮递员(chrome扩展名)中工作正常,但是没有得到正确的JSON对象响应(我发现了这样的字符串响应)

"<!doctype html... "

但返回状态码为200。方法为POST方法,主体为

 { "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"}

我尝试2种方法

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状态代码,但响应不是必需的。

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

暂无
暂无

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

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