我尝试使用凌空连接到API,我设置了所有参数和标题,但似乎这些参数被忽略了,我在这里缺少什么?我上周开始学习机器人凌空,我有点迷失。
package com.rep.app.principal;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.rep.R;
import org.json.JSONObject;
import java.util.HashMap;
import java.util.Map;
public class InicioActivity extends SherlockFragmentActivity {
RequestQueue queue = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
queue=Volley.newRequestQueue(this);
AutenticacaoLocalTask mAutenticacaoLocalTask = new AutenticacaoLocalTask();
mAutenticacaoLocalTask.execute((Void) null);
}
private TextView txtDisplay;
public class AutenticacaoLocalTask extends AsyncTask<Void, Void, Boolean> {
@Override
protected Boolean doInBackground(Void... params) {
try {
txtDisplay = (TextView) findViewById(R.id.txtDisplay);
String url = "http://192.168.1.18/opa/api/";
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkDaaDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8");
return headers;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("email", "rm@test.com.br");
params.put("senha", "aaa");
return params;
}
};
queue.add(jsObjRequest);
return true;
} catch (Exception e) {
Log.e("RM", e.getMessage());
return false;
}
}
@Override
protected void onPostExecute(final Boolean success) {
}
@Override
protected void onCancelled() {
}
}
}
答案 0 :(得分:3)
很明显,有时我们需要在点击网址时提交请求参数。为此,我们必须覆盖getParams()
方法,该方法应返回以键值格式发送的参数列表。
因此,在getParams()
中覆盖JsonObjectRequest
为
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("email", "rm@test.com.br");
params.put("senha", "aaa");
return params;
}
即。使用下面的代码作为
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,url,null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println(response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("TOKEN", "99KI9Gj68CgCf70deM22Ka64chef2C40Gm2lFJ2J0G9JkDaaDAcbFfd19MfacGf3FFm8CM1hG0eDiIk8");
return headers;
}
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("email", "rm@test.com.br");
params.put("senha", "aaa");
return params;
}
};
有关详细信息,请参阅Android working with Volley Library
编辑:
401 是“未授权”的状态代码。如果您在尝试使用HTACCESS时获得 401 ,请参阅此question。您需要使用Authenticator
传递参数。
答案 1 :(得分:0)
不要在请求中放置参数,而是尝试使用JsonObjectRequest
对象的第三个参数,这是一个JSON对象(我的代码中为jsonobj
)。
类似的东西:
JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonobj, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
System.out.println("onResponse()");
...
}
});
检查我的回答here。