我正尝试将请求进行翻新。但改型返回null并运行Failure方法。
问题不是主机。我在邮递员上尝试了该请求,但没有问题。
public class LoginActivity extends AppCompatActivity{
EditText searchEdit;
Button btn;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
searchEdit = (EditText) findViewById(R.id.searchEditXml);
btn.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
FakeTwitterProvider twitterProvider = new FakeTwitterProvider();
MyApi myApi = twitterProvider.getTService();
Fields fields = new Fields();
fields.search = searchEdit.getText().toString();
Call<Fields> call = myApi.createTweet(fields);
call.enqueue(new Callback<Fields>()
{
@Override
public void onResponse(Call<Fields> call, retrofit2.Response<Fields> response)
{
if (response.isSuccessful())
{
Log.e("1", String.valueOf(response.body()));
Toast.makeText(getBaseContext(), "successfull" + response.body(), Toast.LENGTH_SHORT).show();
//String a = response.body().toString();
}
else
{
Log.e("0", String.valueOf(response.body()));
Toast.makeText(getBaseContext(), "http request fail" + response.code(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<Fields> call, Throwable t)
{
Log.e("1","00000000000000000000");
Toast.makeText(getBaseContext(), "fail it" + t.getCause(), Toast.LENGTH_LONG).show();
}
});
}
});}
这是我的改造界面:
public interface MyApi
{
@POST("/news_folder/search.php/")
Call<Fields> createTweet(@Body Fields fields);
}
url变量的url类:
public class Urlclass
{
public static final String url = "http://mywebsite.com/";
}
改装类:
public class FakeTwitterProvider
{
private MyApi mTService;
public FakeTwitterProvider()
{
OkHttpClient httpClient = new OkHttpClient();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl(Urlclass.url)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
mTService = retrofit.create(MyApi.class);
}
public MyApi getTService()
{
return mTService;
}
}
和我的领域:
public class Fields
{
@SerializedName("search")
public String search;
}
哪一部分错了?