我对Android Studio,Java和Stack Overflow完全陌生。我的应用程序使用Volley执行了许多HTTP Post请求,因此,我用代码执行了独立的Java类来执行Post请求。
public class HTTPReq {
String[] finalResponse = new String[1];
public String postRequest(final HashMap<String,String> params, final Context context) {
RequestQueue requestQueue = Volley.newRequestQueue(context);
String url = "https://reqres.in/api/login";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
finalResponse[0] = response;
Toast.makeText(context, "2" + finalResponse[0], Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
finalResponse[1] = error.getMessage();
//Toast.makeText(context, "Response Failed", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
requestQueue.add(stringRequest);
Toast.makeText(context, "3" + finalResponse[0], Toast.LENGTH_LONG).show();
return finalResponse[0];
}
}
我想要实现的是使用return返回http请求对函数调用的响应。函数调用如下:
public void login(String phno, String password,Context context)
{
HashMap<String,String> credentials = new HashMap<String, String>();
credentials.put("email","[email protected]");
credentials.put("password","cityslicka");
HTTPReq httpReq = new HTTPReq();
String response = httpReq.postRequest(credentials,context);
Toast.makeText(context, "1" + response, Toast.LENGTH_LONG).show();
}
我希望很清楚我要实现的目标。请帮我解决一下这个。
您的使用功能界面
public class HTTPReq {
public void postRequest(final HashMap<String, String> params, final Context context, final ResponseCallBack callBack) {
RequestQueue requestQueue = Volley.newRequestQueue(context);
String url = "https://reqres.in/api/login";
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
callBack.onResponse(response);
Toast.makeText(context, response, Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
callBack.onError(error);
Toast.makeText(context, "Response Failed", Toast.LENGTH_LONG).show();
}
}) {
@Override
protected Map<String, String> getParams() {
return params;
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/x-www-form-urlencoded");
return params;
}
};
requestQueue.add(stringRequest);
}
}
calss界面:
import com.android.volley.VolleyError;
public interface ResponseCallBack<T> {
public void onResponse(T response);
public void onError(VolleyError error_response);
}
主要活动:
public class MainActivity extends AppCompatActivity implements ResponseCallBack {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
HTTPReq httpReq = new HTTPReq();
HashMap<String, String> credentials = new HashMap<String, String>();
credentials.put("email", "[email protected]");
credentials.put("password", "cityslicka");
httpReq.postRequest(credentials, this, this);
}
@Override
public void onResponse(Object response) {
Log.e("TAG", "onResponse: " + response);
}
@Override
public void onError(VolleyError error_response) {
Log.e("TAG", "onError: " + error_response);
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句