如何使用GET方法请求将令牌发送到远程服务器?

时间:2018-05-16 00:20:27

标签: android

我是android studio的初学者。我有一个问题,如何使用GET方法请求将令牌ID发送到远程服务器以获取信息,因为我必须通过身份验证才能获取这些信息。 在GET方法下面和错误消息,请帮忙! 1错误消息GET method

3 个答案:

答案 0 :(得分:1)

您可以使用Retrofit Library

的支持

首先,在您的gradle中,添加此行以进行下载。

compile 'com.squareup.retrofit2:retrofit:2.4.0'

其次,在@Body中选择RequestBody的类型

Gson: com.squareup.retrofit2:converter-gson
Jackson: com.squareup.retrofit2:converter-jackson
Moshi: com.squareup.retrofit2:converter-moshi
Protobuf: com.squareup.retrofit2:converter-protobuf
Wire: com.squareup.retrofit2:converter-wire
Simple XML: com.squareup.retrofit2:converter-simplexml
Scalars (primitives, boxed, and String): com.squareup.retrofit2:converter-scalars

然后将其添加到您的gradle中:

compile 'com.squareup.retrofit2:converter-gson:2.4.0'

第三,创建一个接口类以使用@GET方法或其他方法,例如:

public interface CallMethodService {
        @GET("search")
        Call<ResponseModel>  getResponseModel();
    }

最后,在您的MainActivity中:

创建一个改造

Retrofit retrofit = new Retrofit.Builder()
                        .baseUrl("base server link")
                        .addConverterFactory(RequestBody type)
                        .build();

例如:

Retrofit retrofit = new Retrofit.Builder()
                    .baseUrl("http://api.giphy.com/v1/gifs/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();

然后调用获取数据:

 retrofit.create(CallMethodService.class).getResponseModel()
            .enqueue(new Callback<ResponseModel>() {
                @Override
                public void onResponse(Call<ResponseModel> call, Response<ResponseModel> response) {
                   //get data
                }
                @Override
                public void onFailure(Call<GifResponse> call, Throwable t) {
                   // No internet
                }
            });

答案 1 :(得分:1)

您可以尝试使用OkHttp3。 将其添加到您的GRADLE:

compile 'com.squareup.okhttp3:okhttp:3.10.0'

创建一个类并从AsyncTask扩展它 然后从onCreate方法

中调用它
new DataToServer().execute();

DataToServer类实现

private class DataToServer extends AsyncTask<String, Void, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
    }

    @Override
    protected String doInBackground(String... strings) {

        GetDataFromUrl getData = new GetDataFromUrl();
        String response = null;
        try {
            response = getData.run(URL_of_your_server);
        } catch (IOException e) {
            e.printStackTrace();
        }

        return response;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);

        //you got the server response on the result variable

    }

}

这是OkHttp的实现

 private class GetDataFromUrl {
        OkHttpClient client = new OkHttpClient();

        String run(String url) throws IOException {

            RequestBody formBody = new FormBody.Builder()
                    .build();


            Request request = new Request.Builder()
                    .url(url)
                    .post(formBody)
                    .build();

            Response response = null;
            try {
                response = client.newCall(request).execute();
                return response.body().string();
            } finally {
                if (response != null) {
                    response.close();
                }
            }
        }


    }

答案 2 :(得分:0)

我明白了,就是这个:

@覆盖

public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> headers = new HashMap<>();
    headers.put("x-access-token", TokenHandler.getToken());
    return headers;
}