我发现同样的问题 Android Retrofit - POST request doesn't work, but in Postman it works
但我无法解决它
这是gradle
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile 'com.squareup.retrofit2:converter-gson:2.3.0'
compile 'com.squareup.okhttp3:okhttp:3.8.0'
compile 'com.google.code.gson:gson:2.8.0'
这是我在android中发布请求的代码
static final String URL = "http://localhost:3000/";
public void map(){
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(URL)
.addConverterFactory(GsonConverterFactory.create())
.build();
Map map = new HashMap();
map.put("gender", UserSex);
map.put("age", UserAge);
map.put("name", UserName);
map.put("password", UserPw);
map.put("email", UserEmail);
GitHubService gitHubService = retrofit.create(GitHubService.class);
Call<RetrofitRepo> call = gitHubService.getUserItem(map);
call.enqueue(new Callback<RetrofitRepo>() {
@Override
public void onResponse(Call<RetrofitRepo> call, Response<RetrofitRepo> response) {
try {
RetrofitRepo repoList = response.body();
String message = repoList.getmessage();
String token = repoList.gettoken();
if (message.equals("registered successfully")) {
Intent intent = new Intent(UserEnroll.this, LoginActivity.class);
startActivity(intent);
Toast.makeText(context, "register success.", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "register failed.", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onFailure(Call<RetrofitRepo> call, Throwable t) {
}
});
}
这是界面
public interface GitHubService {
@FormUrlEncoded
@POST("api/auth/login")
Call<RetrofitRepo> getPost(
@Field("username") String username
);
@FormUrlEncoded
@POST("api/auth/placeSearch")
Call<RetrofitRepo> getPlaceID(
@Field("placeID") String placeID
);
@FormUrlEncoded
@POST("api/auth/login")
Call<RetrofitRepo> getItem(
@FieldMap Map<String, String> option
);
@FormUrlEncoded
@POST("api/auth/register")
Call<RetrofitRepo> getUserItem(
@FieldMap Map<String, String> option
);
}
我使用节点js这是响应代码
exports.register = (req, res) => {
const { email, password, name, age, gender } = req.body
let newUser = null
const create = (user) => {
if(user) {
throw new Error('email exists')
} else {
return User.create(email, password, name, age, gender)
}
}
const count = (user) => {
newUser = user
return User.count({}).exec()
}
const assign = (count) => {
if(count === 1) {
return newUser.assignAdmin()
} else {
return Promise.resolve(false)
}
}
const respond = (isAdmin) => {
res.json({
message: 'registered successfully',
admin: isAdmin ? true : false
})
}
const onError = (error) => {
res.status(409).json({
message: error.message
})
}
User.findOneByEmail(email)
.then(create)
.then(count)
.then(assign)
.then(respond)
.catch(onError)}
我要求使用邮递员并且有效
http://localhost:3000/api/auth/register
{
"email": "test6@google.com",
"password": "abc123@",
"name": "user",
"age": "18",
"gender": "F"}
POST / api / auth / register 409 13.393 ms - 26或 POST / api / auth / register 200 15.064 ms - 51它在节点
中响应但它不能在我的Android应用程序上使用改造 并且未在节点
中响应答案 0 :(得分:2)
静态最终字符串网址=&#34; http://IPADRESSOFSERVER:3000/&#34;;
对于模拟器使用静态最终字符串网址=&#34; http://10.0.2.2:3000/&#34;;
使用相同的网络连接到本地网络。并使用系统的IP地址。 关闭 系统防火墙和防病毒。
打开浏览器(Chrome推荐)并输入&#34; http://IPADRESSOFSERVER:3000/someWebpageHOstedOnline&#34;。如果可以访问,请检查改装电话。否则设备无法创建与服务器的连接
添加
app.get('/test',function(req,res){
res.send(Hello This is a Test);
});
在您的js文件中
答案 1 :(得分:0)
您需要在基本网址中使用计算机或笔记本电脑的IP地址
static final String URL = "http://yourIPAddress:3000/";
示例:
static final String URL =“http://192.168.0.100:3000/”;