在 android 中获取 Instamojo link
Getting Instamojo link in android
我一直在尝试在我的 android 申请中获取 Instamojo link 进行交易。为了进行测试,我一直在尝试发送 post 请求以生成 link 但我一直收到 AuthFailureError。我正在 post 编写我的代码 below.I 不知道我的代码中是否存在某些错误,或者我按照错误的方式将 Instamojo 集成到我的应用程序中。请帮忙。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private RequestQueue mQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mQueue = CustomVolleyRequestQueue.getInstance(this)
.getRequestQueue();
String url = "https://www.instamojo.com/api/1.1/payment-requests/";
JSONObject params = new JSONObject();
try {
params.put("purpose","selling");
params.put("amount","20");
} catch (JSONException e) {
e.printStackTrace();
}
final CustomJSONObjectRequest jsonRequest=new CustomJSONObjectRequest(Request.Method.POST, url,params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
Log.d("animesh",response.toString());
}
},new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(MainActivity.this, "no internet connection", Toast.LENGTH_SHORT).show();
Log.d("animesh", error.toString());
}
});
mQueue.add(jsonRequest);
}
}
CustomJSONObjectRequest.java:
public class CustomJSONObjectRequest 扩展 JsonObjectRequest {
public CustomJSONObjectRequest(int method, String url, JSONObject jsonRequest,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
super(method, url, jsonRequest, listener, errorListener);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
//String creds = String.format("%s:%s","archerpenny_glide","archer@#62@glide*");
//String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
headers.put("api_key", "**********************************");
headers.put("auth_token", "*******************************");
return headers;
}
@Override
public RetryPolicy getRetryPolicy() {
// here you can write a custom retry policy
return super.getRetryPolicy();
}
}
发送api_key
和你正在做的auth_token
:
headers.put("api_key", "**********************************");
headers.put("auth_token", "*******************************");
相反,您需要这样做:
headers.put("X-Api-Key", "**********************************");
headers.put("X-Auth-Token", "*******************************");
您可以在此处参考 REST API
文档:https://www.instamojo.com/developers/rest/
我一直在尝试在我的 android 申请中获取 Instamojo link 进行交易。为了进行测试,我一直在尝试发送 post 请求以生成 link 但我一直收到 AuthFailureError。我正在 post 编写我的代码 below.I 不知道我的代码中是否存在某些错误,或者我按照错误的方式将 Instamojo 集成到我的应用程序中。请帮忙。
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private RequestQueue mQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
mQueue = CustomVolleyRequestQueue.getInstance(this)
.getRequestQueue();
String url = "https://www.instamojo.com/api/1.1/payment-requests/";
JSONObject params = new JSONObject();
try {
params.put("purpose","selling");
params.put("amount","20");
} catch (JSONException e) {
e.printStackTrace();
}
final CustomJSONObjectRequest jsonRequest=new CustomJSONObjectRequest(Request.Method.POST, url,params, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response)
{
Log.d("animesh",response.toString());
}
},new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Toast.makeText(MainActivity.this, "no internet connection", Toast.LENGTH_SHORT).show();
Log.d("animesh", error.toString());
}
});
mQueue.add(jsonRequest);
} }
CustomJSONObjectRequest.java:
public class CustomJSONObjectRequest 扩展 JsonObjectRequest {
public CustomJSONObjectRequest(int method, String url, JSONObject jsonRequest,
Response.Listener<JSONObject> listener,
Response.ErrorListener errorListener) {
super(method, url, jsonRequest, listener, errorListener);
}
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
//String creds = String.format("%s:%s","archerpenny_glide","archer@#62@glide*");
//String auth = "Basic " + Base64.encodeToString(creds.getBytes(), Base64.DEFAULT);
headers.put("api_key", "**********************************");
headers.put("auth_token", "*******************************");
return headers;
}
@Override
public RetryPolicy getRetryPolicy() {
// here you can write a custom retry policy
return super.getRetryPolicy();
}
}
发送api_key
和你正在做的auth_token
:
headers.put("api_key", "**********************************");
headers.put("auth_token", "*******************************");
相反,您需要这样做:
headers.put("X-Api-Key", "**********************************");
headers.put("X-Auth-Token", "*******************************");
您可以在此处参考 REST API
文档:https://www.instamojo.com/developers/rest/