How to send request Header is “Content-Type”:“appl

2019-02-22 00:28发布

I try to use GET on Volley , but i need send request to application/json .

I take a look for some answers , i try to use jsonBody , but it shows error:

null com.android.volley.ServerError

Here is my code:

public class MainActivity extends AppCompatActivity {

    String url = "http://114.35.246.42:2212/MobileApp/DEST_WebService.asmx/GetNews";
    JSONObject jsonBody;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        try {
            //I try to use this for send Header is application/json
            jsonBody = new JSONObject("{\"type\":\"example\"}");
        } catch (JSONException ex) {
            ex.printStackTrace();
        }

        RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonBody,
                new Response.Listener<JSONObject>() {


                    @Override
                    public void onResponse(JSONObject response) {
                        Log.d("TAG", response.toString());
                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                Log.e("TAG", error.getMessage(), error);
            }


        });


        mQueue.add(jsonObjectRequest);

    }


}

Is any one can teach me how to fix this , any help would be appreciated.

Here is my url: String url = "http://114.35.246.42:2212/MobileApp/DEST_WebService.asmx/GetNews";

4条回答
别忘想泡老子
2楼-- · 2019-02-22 00:43

In general for setting a custom header you need to override getHeaders and set the custom header manually. However, volley handles content type headers differently and getHeaders way does not always work.

So for your case you need to override getBodyContentType. So your code will look like

JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonBody,new Response.Listener<JSONObject>() {

@Override
public void onResponse(JSONObject response) {
   Log.d("TAG", response.toString());
}, new Response.ErrorListener(){
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("TAG", error.getMessage(), error);
        }


    }){
         @Override
         public String getBodyContentType(){
              return "application/json";
         }
    };
查看更多
兄弟一词,经得起流年.
3楼-- · 2019-02-22 00:48

I try to use GET on Volley

The docs for the method you are calling says this

Constructor which defaults to GET if jsonRequest is null, POST otherwise

You can't GET with an HTTP JSON body. Maybe that's the error.

//I try to use this for send Header is application/json
jsonBody = new JSONObject("{\"type\":\"example\"}");

That's not the header, so pass in null here to do GET

new JsonObjectRequest(url, null,

And towards the end of your request, override a method to request JSON

        ... 

        @Override
        public void onErrorResponse(VolleyError error) {
            Log.e("TAG", error.getMessage(), error);
        }


    }) { // Notice no semi-colon here
        @Override 
        public Map<String, String> getHeaders() throws AuthFailureError { 
            Map<String, String> params = new HashMap<String, String>();                
            params.put("Content-Type", "application/json");
            return params; 
        } 
    };
查看更多
一纸荒年 Trace。
4楼-- · 2019-02-22 00:54

Use String request instead of jsonrequest like this

            StringRequest loginMe = new StringRequest(Request.Method.GET, "http://114.35.246.42:2212/MobileApp/DEST_WebService.asmx/GetNews", new Response.Listener<String>() {

                @Override
                public void onResponse(String response) {

                    System.out.println("LoginActivity -- onResponse --> " + response);

                    if (progressDialog != null) {

                        progressDialog.dismiss();

                    }

                    try {

                        JSONObject jsonObject = new JSONObject(response);

                    } catch (Exception e) {

                        CommonUtility.somethingWentWrongDialog(activity,
                                "LoginActivity -- onResponse-- Exception --> ".concat(e.getMessage()));

                    }
                }
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {

                    if (progressDialog != null) {

                        progressDialog.dismiss();

                    }

                    CommonUtility.somethingWentWrongDialog(activity,
                            "LoginActivity -- onErrorResponse --> ".concat(error.getMessage()));

                }
            }) {

                @Override
                protected Map<String, String> getParams() {

                    Map<String, String> params = new HashMap<>();


                    System.out.println("LoginActivity -- LoginParams --> " + params.toString());

                    return params;

                }
            };

            loginMe.setRetryPolicy(new DefaultRetryPolicy(60000, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
                    DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));

            Volley.newRequestQueue(activity).add(loginMe);
查看更多
Lonely孤独者°
5楼-- · 2019-02-22 01:06
@Override 
public Map<String, String> getHeaders() throws AuthFailureError { 
    Map<String, String> params = new HashMap<String, String>();                
    params.put("Content-Type", "application/json");
    return params; 
} 

Implementation in your's

public class MainActivity extends AppCompatActivity {

    String url = "http://114.35.246.42:2212/MobileApp/DEST_WebService.asmx/GetNews";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        RequestQueue mQueue = Volley.newRequestQueue(getApplicationContext());

        JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(url, jsonBody,
            new Response.Listener<JSONObject>() {
                @Override
                public void onResponse(JSONObject response) {
                    Log.d("TAG", response.toString());
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.e("TAG", error.getMessage(), error);
                }
            }) { //no semicolon or coma
            @Override 
            public Map<String, String> getHeaders() throws AuthFailureError { 
                Map<String, String> params = new HashMap<String, String>();                
                params.put("Content-Type", "application/json");
                return params; 
            } 
        };
        mQueue.add(jsonObjectRequest);
    }
}
查看更多
登录 后发表回答