我尝试将服务器与我的Android应用程序连接 我想访问我的api,通过post json对象发出一个POST请求。
{
"username" : "admin",
"rememberMe" : true,
"password" : "admin"
}
如果用户确实存在要连接,则Web服务会为您提供id_token。 Web服务已经存在。我尝试将Volley库用于Android应用程序。
这是我的源代码
public class MainActivity extends AppCompatActivity {
EditText usermail;
EditText userPassword;
Button signInButton;
TextView signUpTextview;
Button signInFacebook;
byte userType;
String loginApi="http://192.168.100.28:8080/api/authenticate";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
usermail = (EditText)findViewById(R.id.signInMail);
userPassword = (EditText)findViewById(R.id.signInPassword);
signInButton = (Button)findViewById(R.id.signInButton);
signUpTextview = (TextView)findViewById(R.id.signUpTextView);
signInFacebook = (Button)findViewById(R.id.signInFacebook);
signInButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
boolean connected = ConnectionService.checkConnection(getApplicationContext());
if(!connected){
ToastOX.Tnull(getApplicationContext(), "A network error has occurred");
return;
}
Map<String, String> params = new HashMap<String, String>();
params.put("username", "admin");
params.put("rememberMe", "true");
params.put("password", "password");
JSONObject jsonObj = new JSONObject(params);
JsonObjectRequest stringRequest = new JsonObjectRequest(Request.Method.POST, loginApi,jsonObj ,new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
String id_token = null;
try {
id_token = response.getString("id_token");
Log.d("nizarab",id_token);
} catch (JSONException e) {
Log.d("nizarab","erreur");
e.printStackTrace();
}
Toast.makeText(getApplicationContext(),id_token,Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
//TODO: handle failure
Log.e("!_@@_Error::>",error.toString());
}
});
VolleyParce.getInstance(MainActivity.this).add(stringRequest);
}
});
} }
但我没有收到id_token 这是我的日志错误
01-24 17:32:07.051 26319-27109 / com.nizar.talquest10 E / Volley:[40066] BasicNetwork.performRequest:http://192.168.100.28:8080/api/authenticate的意外响应代码401 01-24 17:32:07.051 26319-27114 / com.nizar.talquest10 E / Volley:[40071] BasicNetwork.performRequest:http://192.168.100.28:8080/api/authenticate的意外响应代码401 01-24 17:32:07.051 26319-26319 / com.nizar.talquest10 W / System.err:com.android.volley.AuthFailureError 01-24 17:32:07.061 26319-26319 / com.nizar.talquest10 W / System.err:at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:171) 01-24 17:32:07.061 26319-26319 / com.forsyslab.talquest10 W / System.err:at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:114) 01-24 17:32:07.061 26319-26319 / com.forsyslab.talquest10 E /!_ @@ _ Error ::&gt;:com.android.volley.AuthFailureError