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";
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 andgetHeaders
way does not always work.So for your case you need to override
getBodyContentType
. So your code will look likeThe docs for the method you are calling says this
You can't GET with an HTTP JSON body. Maybe that's the error.
That's not the header, so pass in null here to do GET
And towards the end of your request, override a method to request JSON
Use String request instead of jsonrequest like this
Implementation in your's