我正在实现登录功能,并使用Post请求,但我收到错误说
"retrofit.RetrofitError:com.squareup.okhttp.internal.http.HttpMethod.METHODS"
以下是我的代码
import java.util.HashMap;
import java.util.Map;
import retrofit.Callback;
import retrofit.http.*;
//Myapi.java
import java.util.HashMap;
import java.util.Map;
import retrofit.Callback;
import retrofit.http.*;
public interface MyApi {
/* LOGIN */
@POST("/api/0.01/oauth2/access_token/")
// your login function in your api
public void login(@Body HashMap<String, String> arguments, Callback<String> calback);
}
//In my activity
RestAdapter restAdapter = new RestAdapter.Builder()
.setEndpoint(Constants_Interface.URL).setClient(newclient)
.build();
MyApi mylogin = restAdapter.create(MyApi.class);
HashMap<String, String> dicMap = new HashMap<String, String>();
dicMap.put("client_id", XXX);
dicMap.put("client_secret", XXX);
dicMap.put("username", XXX);
dicMap.put("password", XXX);
mylogin.login(dicMap, new Callback<String>() {
@Override
public void …Run Code Online (Sandbox Code Playgroud) 我提到了这个链接,但我似乎无法为我实施
我在用
compile 'com.squareup.retrofit2:retrofit:2.0.2'
compile 'com.squareup.retrofit2:converter-gson:2.0.2'
Run Code Online (Sandbox Code Playgroud)
我使用下面的代码,如何为此设置超时!
public class ApiClient {
public static final String BASE_URL = Constants.BaseURL;
private static Retrofit retrofit = null;
public static Retrofit getClient() {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
Run Code Online (Sandbox Code Playgroud) 我有一个像下面的邮递员的形象.如何在Retrofit 2中做同样的事情.
我已经宣布了这样的界面.
@Multipart
@POST("/api/Pharmarcy/UploadImage")
Call<ResponseBody> uploadPrescriptionImage(
@Query("accessToken") String token,
@Query("pharmarcyRequestId") int pharmacyRequestedId,
@Part MultipartBody.Part image);
Run Code Online (Sandbox Code Playgroud) 我叫PATCH Web服务使用Retrofit2但onResponse不叫和onFailure处被称为尽管该服务的操作是成功的在服务器端完美
无论何时,我都试图使用fiddler来检查它的工作,我发现问题是序列化即将到来的服务响应,当使用fiddler时,我发现没有JSON响应的内容,所以Retrofit服务认为它失败了,因为没有内容,它无法序列化EMPTY内容并给我这个错误
java.io.EOFException: End of input at line 1 column 1
Run Code Online (Sandbox Code Playgroud)
提琴手原始回应
HTTP/1.1 200 OK
Server: nginx/1.9.4
Date: Wed, 02 Mar 2016 09:55:55 GMT
Content-Type: application/json
Content-Length: 0
Connection: close
Status: 200 OK
X-Content-Type-Options: nosniff
Run Code Online (Sandbox Code Playgroud)
Fiddler Json的回应是空的
java中的webservice
Call<Object> call = TimeCapp.services.accept_invited_alerts(HomeActivity.api_token, alert_id);
call.enqueue(new Callback<Object>()
{
@Override
public void onResponse (Call<Object> call, Response<Object> response)
{
if (response.isSuccess()) {
String x = response.body();
}
}
@Override
public void onFailure (Call<Object>call, Throwable t) …Run Code Online (Sandbox Code Playgroud) 我正在开发Youtube API.基本网址为https://www.googleapis.com/youtube/v3/search/ 请求:GET https://www.googleapis.com/youtube/v3/search?part=snippet&q= {search_keyword}&key = {API_键}
ApiService接口代码 -
https://www.googleapis.com/youtube/v3/search?part=snippet&q={search_keyword}&key={API_KEY}
Run Code Online (Sandbox Code Playgroud)
错误:java.lang.IllegalArgumentException:缺少@GET URL或@Url参数. 在代码行中
public interface ApiService {
@GET("")
Call<YoutubeResponse> searchVideos(@Query("part") String part,
@Query("q") String q,@Query("key") String apiKey);
}
Run Code Online (Sandbox Code Playgroud)
我是初学者.请帮忙!
我的问题是我不知道如何开始使用收到的API的Retrofit 2.0 - 下面提到...
首先,我需要用户名,密码,fbID(可选),gmailID(可选),twitID(可选),性别,birthDate,位置(不需要 - 如果long和lat有值),经度(可选),纬度(可选) ,profileImage(可选).
当所有参数都很好时 - 接收status = true.如果不是 - 接收status = false和所需的参数错误(例如已经收到邮件)
所以我可以接收
status = true
或者
status = false使用最多5个参数(用户名,电子邮件,密码,性别,birthDate)进行数组.
我试过这个API Interface:
public interface AuthRegisterUserApi {
@PUT()
Call<AuthRegisterUserModel> getStatus(
@Field("username") String username,
@Field("email") String email,
@Field("password") String password,
@Field("fbID") String fbID,
@Field("gmailID") String gmailID,
@Field("twitID") String twitID,
@Field("gender") String gender,
@Field("birthDate") String birthDate,
@Field("location") String location,
@Field("longitude") String longitude,
@Field("latitude") String latitude,
@Field("profileImage") String profileImage
);
class …Run Code Online (Sandbox Code Playgroud) 我正在尝试将基本身份验证(用户名和密码)添加到Retrofit OkHttp客户端.这是我到目前为止的代码:
private static Retrofit createMMSATService(String baseUrl, String user, String pass) {
HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.client(client)
.addConverterFactory(GsonConverterFactory.create())
.build();
return retrofit;
}
Run Code Online (Sandbox Code Playgroud)
我正在使用Retrofit 2.2,本教程建议使用AuthenticationInterceptor,但此类不可用.添加凭据的正确位置在哪里?我是否必须将它们添加到我的拦截器,客户端或Retrofit对象中?我该怎么做?
我正在使用以下代码行为使用Retrofit2发送的所有请求添加默认标头:
private static OkHttpClient defaultHttpClient = new OkHttpClient();
static {
defaultHttpClient.networkInterceptors().add(new Interceptor() {
@Override
public Response intercept(Chain chain) throws IOException {
Request request = chain.request().newBuilder()
.addHeader("Accept", "Application/JSON").build();
return chain.proceed(request);
}
});
}
Run Code Online (Sandbox Code Playgroud)
在将改造升级到beta-3版本之后,我还必须将OkHttp升级到OkHttp3(实际上我只是将包名称从okhttp更改为okhttp3,该库包含在改造中).之后我从这一行得到例外:
defaultHttpClient.networkInterceptors().add(new Interceptor());
Run Code Online (Sandbox Code Playgroud)
引起:java.util.Collections的java.lang.UnsupportedOperationException $ UnmodifiableCollection.add(Collections.java:932)
引起:java.lang.ExceptionInInitializerError
这里有什么问题?
我正在做android,寻找一种方法来做一个超级基本的http GET/POST请求.我一直收到错误:
java.lang.IllegalArgumentException: Unable to create converter for class java.lang.String
Run Code Online (Sandbox Code Playgroud)
网络服务:
public interface WebService {
@GET("/projects")
Call<String> jquery();
}
Run Code Online (Sandbox Code Playgroud)
然后在我的java中:
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://jquery.org")
// .addConverterFactory(GsonConverterFactory.create())
.build();
WebService service = retrofit.create(WebService.class);
Call<String> signin = service.jquery();
Toast.makeText(this, signin.toString(), Toast.LENGTH_LONG).show();
Run Code Online (Sandbox Code Playgroud)
我实际上只是尝试使用GET请求查询jquery.org/projects并返回它响应的String.怎么了?
如果我尝试实现一个自定义转换器(我在网上找到了一些例子),它抱怨我没有实现抽象方法convert(F),这些都没有.
谢谢.
我正在使用改造从“ https://jsonplaceholder.typicode.com/comments ”获取示例数据,但出现此错误:
2020-03-31 16:33:12.011 8140-8140/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.retrofit_tutorial, PID: 8140
java.lang.BootstrapMethodError: Exception from call site #4 bootstrap method
at okhttp3.internal.Util.<clinit>(Util.java:87)
at okhttp3.internal.Util.skipLeadingAsciiWhitespace(Util.java:321)
at okhttp3.HttpUrl$Builder.parse(HttpUrl.java:1313)
at okhttp3.HttpUrl.get(HttpUrl.java:917)
at retrofit2.Retrofit$Builder.baseUrl(Retrofit.java:492)
at com.example.retrofit_tutorial.MainActivity.onCreate(MainActivity.java:29)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
Run Code Online (Sandbox Code Playgroud)
我的 MainActivity.java 是:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
requestPermissions(new String[]{Manifest.permission.INTERNET},0);
textView = findViewById(R.id.text_view);
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://jsonplaceholder.typicode.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
JsonPlaceHolderApi jsonPlaceHolderApi = retrofit.create(JsonPlaceHolderApi.class);
Call<List<Comments>> call= jsonPlaceHolderApi.getComments();
call.enqueue(new Callback<List<Comments>>() {
@Override …Run Code Online (Sandbox Code Playgroud)