我是想做一个建议的retrofit请求以及一个javaweb服务器来响应,分别用的是androidstudio和Intellij
下面是代码:
客户端:
public interface Get_Interface {
@GET("/hello?username=cwl")
Call<UserName> login();
}
自定义的responsebody
```java
public class UserName {
private String name;
public void setName(String name) {
this.name = name;
}
public String getName() {
return name;
}
}
```java
package com.example.apistudy;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class PostRequest extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_post_request1);
}
private void request() {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://192.168.0.103:80/apidemo/")
.addConverterFactory(GsonConverterFactory.create())
.build();
Get_Interface pi = retrofit.create(Get_Interface.class);
Call<UserName> call = pi.login();
call.enqueue(new Callback<UserName>() {
@Override
public void onResponse(Call<UserName> call, Response<UserName> response) {
if (response.isSuccessful())
{
Log.d("success", response.toString());
}
else
{
try
{
Log.d("error1", response.toString());
}
catch (Exception e)
{
e.printStackTrace();
Log.d("error2", e.toString());
}
}
}
@Override
public void onFailure(Call<UserName> call, Throwable t) {
System.out.println("小失败");
System.out.println(t.getMessage());
}
});
}
public void Get(View view) {
request();
}
}
Get方法是button按钮Onclick调用的
接下来是服务端
import com.alibaba.fastjson.JSONObject;
import com.sun.deploy.net.HttpRequest;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Map;
@WebServlet("/hello")
public class helloRequest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("aaaa");
req.setCharacterEncoding("UTF-8");
resp.setContentType("application/json; charset=utf-8");
String username = req.getParameter("username");
JSONObject json = new JSONObject();
try {
if(username.equals("cwl")){
json.put("name", "yes");
}else{
json.put("name", "no");
}
} catch (Exception e) {
e.printStackTrace();
}finally{
PrintWriter write = resp.getWriter();
write.write(json.toString());
}
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
现在的问题就是安卓端运行retrofit返回onResponse方法,但是response.body()是null。response.issucessful()返回false,输出类似
D/error1: retrofit2.Response@8da6216
retrofit请求服务端完全没有反应,但是在安卓虚拟机上网页访问返回正常,电脑网页访问返回json数据也正常。现在项目卡在这里,希望懂的同志可以讲讲是哪个地方出了问题。