我正在使用Retrofit2来连接我用NodeJS编写的API。当我试图在服务器上发布数据时我正在使用MongoDB数据库
HTTP 503 service unavailable
这些是我的依赖项:
implementation 'com.squareup.retrofit2:retrofit:2.5.0'
implementation 'com.squareup.retrofit2:converter-gson:2.5.0'
implementation 'io.reactivex.rxjava2:rxjava:2.2.9'
implementation 'io.reactivex.rxjava2:rxandroid:2.1.1'
implementation 'com.squareup.retrofit2:adapter-rxjava2:2.3.0'
这是我的代码:
BioData.class
public class BioData {
@SerializedName("name")
@Expose
private String name;
@SerializedName("age")
@Expose
private String age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
RetrofitClient.java
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getInstance(){
if(retrofit == null)
retrofit = new Retrofit.Builder()
.baseUrl("https://www.example.com/")
.addConverterFactory(GsonConverterFactory.create())
.addCallAdapterFactory(RxJava2CallAdapterFactory.create())
.build();
return retrofit;
private RetrofitClient(){
}
}
}
ApiService.java
public interface ApiService {
@POST("retrofitUsers")
@FormUrlEncoded
Observable<BioData> saveData(@Field("name") String name,
@Field("age") String age);
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
EditText name,age;
Button submit;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
name = findViewById(R.id.name);
age = findViewById(R.id.age);
submit = findViewById(R.id.submit);
progressBar = findViewById(R.id.progressBar);
submit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressBar.setVisibility(View.VISIBLE);
String str1 = name.getText().toString();
String str2 = age.getText().toString();
if(str1.equals("")){
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(),"Enter name",Toast.LENGTH_SHORT).show();
}
else if(str2.equals("")){
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(),"Enter age",Toast.LENGTH_SHORT).show();
}
else{
postData(str1,str2);
}
}
});
}
private void postData(String str1,String str2){
Retrofit retrofit = RetrofitClient.getInstance();
ApiService mapiService = retrofit.create(ApiService.class);
mapiService.saveData(str1,str2)
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.unsubscribeOn(Schedulers.io())
.subscribe(new Observer<BioData>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(BioData bioData) {
}
@Override
public void onError(Throwable e) {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_SHORT).show();
}
@Override
public void onComplete() {
progressBar.setVisibility(View.INVISIBLE);
Toast.makeText(getApplicationContext(), "Data inserted successfully", Toast.LENGTH_SHORT).show();
}
});
}
}
请让我知道我在上面的代码中做错了什么,我们将不胜感激。
谢谢
答案 0 :(得分:0)
对于您的附加评论问题,请替换:
.addConverterFactory(GsonConverterFactory.create())
到
.addConverterFactory(GsonConverterFactory.create(new GsonBuilder().setLenient().create()))