我尝试在Volley中使用GET方法,但我需要发送一个请求到
application/json
。我查找了一些答案,尝试使用jsonBody
,但出现错误:
这是我的代码:null com.android.volley.ServerError
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);
}
}
有人能教我如何解决这个问题吗?非常感谢任何帮助。
这是我的url:
String url = "http://114.35.246.42:2212/MobileApp/DEST_WebService.asmx/GetNews";
jsonBody
是请求的 消息体,而不是请求头。由于您遇到了一个服务器错误,所以应该阅读某个日志信息中的内容,而不是在 logcat 中查看。 - OneCricketeer