Android截击响应获取数据
在字符串post中请求登录。在onResponse中,我返回的是状态代码200。我也想得到返回的数据。我不知道从这一点开始该往哪里走?关于如何获取返回的数据,而不仅仅是状态码,有什么想法吗Android截击响应获取数据,android,android-volley,jsonresponse,Android,Android Volley,Jsonresponse,在字符串post中请求登录。在onResponse中,我返回的是状态代码200。我也想得到返回的数据。我不知道从这一点开始该往哪里走?关于如何获取返回的数据,而不仅仅是状态码,有什么想法吗 public void requestWithSomeHttpHeaders() { try { RequestQueue requestQueue = Volley.newRequestQueue(this); String URL = "http://..
public void requestWithSomeHttpHeaders() {
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://......";
JSONObject jsonBody = new JSONObject();
jsonBody.put("username", "yourusername");
jsonBody.put("password", "yourpassword");
final String mRequestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
@Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
@Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
@Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
System.out.println(responseString);
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
public void requestWithSomeHttpHeaders(){
试一试{
RequestQueue RequestQueue=Volley.newRequestQueue(this);
字符串URL=”http://......";
JSONObject jsonBody=新的JSONObject();
放置(“用户名”、“你的用户名”);
放置(“密码”、“您的密码”);
最后一个字符串mRequestBody=jsonBody.toString();
StringRequest StringRequest=new StringRequest(Request.Method.POST,URL,new Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
Log.i(“截击”,回应);
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
Log.e(“截击”,error.toString());
}
}) {
@凌驾
公共字符串getBodyContentType(){
返回“application/json;charset=utf-8”;
}
@凌驾
公共字节[]getBody()抛出AuthFailureError{
试一试{
返回mRequestBody==null?null:mRequestBody.getBytes(“utf-8”);
}捕获(不支持的编码异常uee){
wtf(“尝试使用%s获取%s的字节时不支持编码”,mRequestBody,“utf-8”);
返回null;
}
}
@凌驾
受保护的响应parseNetworkResponse(NetworkResponse响应){
字符串responseString=“”;
if(响应!=null){
responseString=String.valueOf(response.statusCode);
//可以获取更多详细信息,如response.headers
系统输出打印LN(响应预算);
}
返回Response.success(responseString,HttpHeaderParser.parseCacheHeaders(Response));
}
};
添加(stringRequest);
}捕获(JSONException e){
e、 printStackTrace();
}
}
最好定义一种方法
private void doLogin() {
// TODO: startActivity?
}
那就这么说吧
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
doLogin();
}
StringRequest-StringRequest=new-StringRequest(Request.Method.POST,URL,new-Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
Log.i(“截击”,回应);
多洛金();
}
如果您想从服务器获取数据,这就是String response
的含义。请检查日志
如注释中所述,使用
StringRequest
而不实现getBody
、parseNetworkResponse
和getBodyContentType
可能会更好。最好定义一个方法
private void doLogin() {
// TODO: startActivity?
}
那就这么说吧
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
doLogin();
}
StringRequest-StringRequest=new-StringRequest(Request.Method.POST,URL,new-Response.Listener()){
@凌驾
公共void onResponse(字符串响应){
Log.i(“截击”,回应);
多洛金();
}
如果您想从服务器获取数据,这就是String response
的含义。请检查日志
如注释中所述,使用
StringRequest
而不实现getBody
、parseNetworkResponse
和getBodyContentType
可能会更好。如果要解析JSON,请添加这样的方法
public Profile getUserProfile(String response){
Profile profile = null;
try {
profile = new Profile();
JSONObject jsonObject = new JSONObject(response);
profile.setId(jsonObject.getInt(context.getString(R.string.profile_id)));
profile.setLastName(jsonObject.getString(context.getString(R.string.profile_lastName)));
profile.setName(jsonObject.getString(context.getString(R.string.profile_name)));
profile.setEmail(jsonObject.getString(context.getString(R.string.profile_email)));
} catch (JSONException | NullPointerException e) {
e.printStackTrace();
return null;
}
return profile;
}
在您的onResponse方法中
@Override
public void onResponse(String response) {
//If you have son object
Profile profile = getUserProfile(response)
}
如果要解析JSON,请添加如下方法
public Profile getUserProfile(String response){
Profile profile = null;
try {
profile = new Profile();
JSONObject jsonObject = new JSONObject(response);
profile.setId(jsonObject.getInt(context.getString(R.string.profile_id)));
profile.setLastName(jsonObject.getString(context.getString(R.string.profile_lastName)));
profile.setName(jsonObject.getString(context.getString(R.string.profile_name)));
profile.setEmail(jsonObject.getString(context.getString(R.string.profile_email)));
} catch (JSONException | NullPointerException e) {
e.printStackTrace();
return null;
}
return profile;
}
在您的onResponse方法中
@Override
public void onResponse(String response) {
//If you have son object
Profile profile = getUserProfile(response)
}
是否真的有必要实现更多的
onResponse
和onError
?是否真的有必要实现更多的onResponse
和onError
?在logcat中,我看到的只是i/VOLLEY:200如果我在Google Postman中做了类似的事情,我会收到各种数据。我不知道你的URL是什么,但我知道t似乎只是返回“200”
。如果这是一个问题,那么这一切都是服务器端的问题,而不是使用StringRequest
而不实现getBody
或parseNetworkResponse
的凌乱问题。谢谢!我的API当前需要在正文中发送用户名和密码。我删除了parseNetworkResponse,并修改了getBody以返回mRequestBody.toString().getBytes();在logcat中我看到的是i/VOLLEY:200如果我在Google Postman中做了类似的操作,我会返回各种数据。我不知道你的URL是什么,但它似乎只是返回了“200”
。如果这是一个问题,那么这一切都是服务器端的问题,而不是使用StringRequest
而不实现getBody
或parseNetworkResponse
的凌乱问题。谢谢!我的API当前需要在正文中发送用户名和密码。我删除了parseNetworkResponse,并修改getBody以返回mRequestBody.toString().getBytes();