REST Web服务在邮递员中找到了响应但未在排队中发现,但找到的状态代码为200

时间:2017-01-13 12:56:42

标签: javascript android json rest

我是Android的初学者。我尝试使用volley在Android中连接其他Web服务。它在postman(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状态代码,但响应不是必需的。

1 个答案:

答案 0 :(得分:0)

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