我一直试图从LH公共API中生成访问令牌,但是当我使用Volley发送POST请求时,我得到的是"com.android.Volley.AuthFailureError“。
package jvtech.jasvir;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONException;
import org.json.JSONObject;
public class MainActivity extends AppCompatActivity {
EditText latitude,longitude;
Button submit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latitude=(EditText)findViewById(R.id.lat);
longitude=(EditText)findViewById(R.id.lon);
submit=(Button)findViewById(R.id.submit);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url="https://api.lufthansa.com/v1/oauth/token";
String key="abc";
String secret="abc";
String grant_type= "abc";
JSONObject object=new JSONObject();
try{
object.put("client_id",key);
object.put("client_secret",secret);
object.put("grant_type",grant_type);
}
catch(JSONException e)
{
e.printStackTrace();
}
RequestQueue queue= Volley.newRequestQueue(MainActivity.this);
JsonObjectRequest request=new JsonObjectRequest(Request.Method.POST, url, object, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try
{
Toast.makeText(MainActivity.this, ""+response.getString("access_token"), Toast.LENGTH_SHORT).show();
}
catch (JSONException e)
{
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, ""+error, Toast.LENGTH_SHORT).show();
}
});
queue.add(request);
}
});
}}在收到令牌后,我想创建一个应用程序,根据输入的坐标显示最近的机场。到目前为止,这些纬度和经度变量与此无关。请帮帮忙。提前谢谢。祝您今天愉快。
发布于 2018-07-11 03:53:57
首先,我不认为REST接受JSON对象作为请求数据。如果你看到他们的文档,上面写着"Content-Type: application/x-www-form-urlencoded"。这意味着数据应该以key1=value1&key2=value2...的形式提供。
将此作为发送x-www-form-uriencoded类型请求的参考
在映射内部(我们使用Map设置Key=value类型的params),使用这些设置请求参数
param.put("client_id","your client id");
param.put("client_secret", "your secret");
param.put("grant_type","client_credentials");希望这能帮上忙。万事如意
https://stackoverflow.com/questions/51269342
复制相似问题