当我做POST请求调用使用JsonObjectRequest在凌空然后在某些wifi它是发送空的身体。但是它可以很好地处理StringRequest POST请求。它在所有移动网络上都运行良好。
我正在使用node。js服务器和expressjs/body解析器。当我使用邮递员发出邮件请求时,一切正常。
错误是什么?如果有人想看我能提供的代码。
使用StringRequest的POST请求
private void LoginUser(final String email,final String pass) {
try {
/*Map<String, String> params = new HashMap<String, String>();
params.put("username", email);
params.put("password", pass);
JSONObject jsonParams = new JSONObject(params);*/
final StringRequest stringRequest = new StringRequest(Request.Method.POST, REGISTER_URL,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Toast.makeText(getApplicationContext(),response.toString(),Toast.LENGTH_LONG).show();
try
{
Log.i("Inside try", "yes");
JSONObject jsonResponse = new JSONObject(response).getJSONObject("user");
Log.i("User name",jsonResponse.getString("name"));
Constants.setClinicName(jsonResponse.getString("name"));
String TokenDB=new JSONObject(response).getString("token");
//Toast.makeText(getApplicationContext(),TokenDB,Toast.LENGTH_LONG).show();
if(pd.isShowing())
{
pd.dismiss();
}
//Maintaining LogIn data till user clicks LogOut
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("Token",TokenDB);
//editor.putString("username", username);
editor.commit();
//Printing Token in Log in case of null token debugging
/*String status=manager.getPreferences(ClinicLogin.this,"token");
Log.d("token", status);
*/
Intent intent=new Intent(ClinicLogin.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}catch (JSONException ks)
{
ks.printStackTrace();
Toast.makeText(getApplicationContext(),
"Oops! The Username & Password Do Not Match. Please try again!",
Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle Error
if(pd.isShowing()) {pd.dismiss();}
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
error.printStackTrace();
Toast.makeText(getApplicationContext(), "Network Error", Toast.LENGTH_SHORT).show();
} else if (error instanceof AuthFailureError) {
//TODO
error.printStackTrace();
Toast.makeText(getApplicationContext(), "User not authorized", Toast.LENGTH_SHORT).show();
} else if (error instanceof ServerError) {
//TODO
error.printStackTrace();
Toast.makeText(getApplicationContext(), "Server error", Toast.LENGTH_SHORT).show();
} else if (error instanceof NetworkError) {
//TODO
error.printStackTrace();
Toast.makeText(getApplicationContext(), "Network Error", Toast.LENGTH_SHORT).show();
} else if (error instanceof ParseError) {
//TODO
error.printStackTrace();
Toast.makeText(getApplicationContext(), "Error consuming request", Toast.LENGTH_SHORT).show();
}
else error.printStackTrace();
}
}){
@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("username", email);
params.put("password", pass);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
stringRequest.setRetryPolicy(new DefaultRetryPolicy(
7000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(stringRequest);
} catch (Exception e) {
e.printStackTrace();
}
}
POST调用使用JsonObjtRequest
private void LoginUser(String email,String pass) {
try {
Map<String, String> params = new HashMap<String, String>();
params.put("username", email);
params.put("password", pass);
JSONObject jsonParams = new JSONObject(params);
JsonObjectRequest postRequest = new JsonObjectRequest(Request.Method.POST, REGISTER_URL, jsonParams,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
// Parsing json object response
// response will be a json object
String TokenDB = response.getString("token");
JSONObject user=response.getJSONObject("user");
Constants.setClinicName(user.getString("name"));
//Constants.setTokenDB(TokenDB);
if(pd.isShowing())
{
pd.dismiss();
}
//Maintaining LogIn data till user clicks LogOut
SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = app_preferences.edit();
editor.putString("Token",TokenDB);
//editor.putString("username", username);
editor.commit();
//Printing Token in Log in case of null token debugging
/*String status=manager.getPreferences(ClinicLogin.this,"token");
Log.d("token", status);
*/
Intent intent=new Intent(ClinicLogin.this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
catch (JSONException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(),
"Oops! The Username & Password Do Not Match. Please try again!",
Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Handle Error
if(pd.isShowing()) {pd.dismiss();}
if (error instanceof TimeoutError || error instanceof NoConnectionError) {
error.printStackTrace();
Toast.makeText(getApplicationContext(), "Network Error", Toast.LENGTH_SHORT).show();
} else if (error instanceof AuthFailureError) {
//TODO
error.printStackTrace();
Toast.makeText(getApplicationContext(), "User not authorized", Toast.LENGTH_SHORT).show();
} else if (error instanceof ServerError) {
//TODO
error.printStackTrace();
Toast.makeText(getApplicationContext(), "Server error", Toast.LENGTH_SHORT).show();
} else if (error instanceof NetworkError) {
//TODO
error.printStackTrace();
Toast.makeText(getApplicationContext(), "Network Error", Toast.LENGTH_SHORT).show();
} else if (error instanceof ParseError) {
//TODO
error.printStackTrace();
Toast.makeText(getApplicationContext(), "Error consuming request", Toast.LENGTH_SHORT).show();
}
else error.printStackTrace();
}
}) {
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json; charset=utf-8");
return headers;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
postRequest.setRetryPolicy(new DefaultRetryPolicy(
7000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
requestQueue.add(postRequest);
} catch (Exception e) {
e.printStackTrace();
}
}
package com.example.mwakidoshi;
import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.HttpHeaderParser;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.UnsupportedEncodingException;
import java.util.Map;
/**
* Created by Sly on 2017-11-17.
*/
public class CustomRequest extends Request<JSONObject> {
private Response.Listener<JSONObject> listener;
private Map<String, String> params;
public CustomRequest(String url, Map<String, String> params, Response.Listener<JSONObject> responseListener, Response.ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.listener = responseListener;
this.params = params;
}
public CustomRequest(int method, String url, Map<String, String> params, Response.Listener<JSONObject> reponseListener, Response.ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
@Override
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) {
listener.onResponse(response);
}
}
我正在使用截击与API交互。我需要向返回JSON数组的服务发送post请求(带参数)。 JsonObjectRequest有一个构造函数,它接受一个方法和一组参数 但是JSONArrayRequest(我需要的一个)只有一个构造函数的形式 我如何才能使其发送包含数据的POST请求?
我有curl请求从Android推送数据到服务器。下面是Curl请求: 我应该如何使用截击进行post请求?我很困惑,因为我不知道该放在哪里: 所以请任何人帮我完成这件事。任何答案都是可贵的。非常感谢。安静!
我正在开发一个Android应用程序,它与我编写的RESTful web服务进行通信。对于方法使用是很棒的,也是很容易的,但是我不能把手指放在方法上。 我想发送一个请求,请求正文中包含一个,并检索web服务的原始响应(如、)。
我正在用GoogleVolley Api创建一个android应用程序,它处理我的应用程序和php脚本之间的连接。出于某种原因,该应用程序没有向我的服务器发送任何数据,也许这里的某个人可以找到它? 我的方法被正确调用,但是当我尝试打印它时,它返回null!一切实际上都在工作,除了数据没有正确传递,我相信,因为我的php脚本抱怨变量为空
我正在制作一个Android应用程序,需要发送帖子请求到我的服务器,真的需要一些帮助!我一直试图使用Android Volley提出这些请求。我已经读了很多相关的问题在这里和各种有关Android Volley的文档来源,但当我设法使我的请求,它是不可能的,我收到发布的数据正确的方式在服务器端,我没有线索,我在做什么错误的...我尝试了stringRequest覆盖getParams()和自定义J
用简单的话来说,我想发送这个数据到服务器,使用Volley Post Request。 它有一个整数和一个字符串,我得到的响应是Jsonarray 我尝试过以下方法,但都不管用 > 因为它是json数组请求,我不能在参数中发送数据,因为第三个参数只需要JsonArray,我必须发送JsonObject,所以保持为null 我不能把它放在中,因为值的1是整数,并且它只接受字符串 getparams(