Retrofit return null












1















i'm trying to post a request to my host with retrofit. but retrofit return null and on Failure method runs.



The problem is not hosts. I tried the request on postman and there is no problem with it.



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();
}
});


}
});}


this is my interface for retrofit:



public interface MyApi
{
@POST("/news_folder/search.php/")
Call<Fields> createTweet(@Body Fields fields);
}


url class for url variable:



public class Urlclass
{
public static final String url = "http://mywebsite.com/";
}


retrofit class:



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;
}
}


and my field:



public class Fields
{
@SerializedName("search")
public String search;
}


Which part is wrong?










share|improve this question























  • So, the onFailure method is being triggered? what is the cause?

    – Barns
    Nov 14 '18 at 17:36











  • Yes. The onFailure method is run. "fail it" messege and "null" showing in toast. i don,t know why.

    – Farid
    Nov 14 '18 at 17:53








  • 1





    if your are combining the url = "http://mywebsite.com/" and @POST("/news_folder/search.php/") it seems to me the result would be url = "http://mywebsite.com//news_folder/search.php/"). Therefore the double "//" could be the issue?

    – Barns
    Nov 14 '18 at 18:09













  • I tried it, but it still has the same result.

    – Farid
    Nov 14 '18 at 18:23











  • Do not only Toast error messages. Use Log.e("tag", "Error ", t); this way you can find exact cause for the error. And edit the question, post error logs here.

    – Shashanth
    Nov 15 '18 at 3:51
















1















i'm trying to post a request to my host with retrofit. but retrofit return null and on Failure method runs.



The problem is not hosts. I tried the request on postman and there is no problem with it.



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();
}
});


}
});}


this is my interface for retrofit:



public interface MyApi
{
@POST("/news_folder/search.php/")
Call<Fields> createTweet(@Body Fields fields);
}


url class for url variable:



public class Urlclass
{
public static final String url = "http://mywebsite.com/";
}


retrofit class:



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;
}
}


and my field:



public class Fields
{
@SerializedName("search")
public String search;
}


Which part is wrong?










share|improve this question























  • So, the onFailure method is being triggered? what is the cause?

    – Barns
    Nov 14 '18 at 17:36











  • Yes. The onFailure method is run. "fail it" messege and "null" showing in toast. i don,t know why.

    – Farid
    Nov 14 '18 at 17:53








  • 1





    if your are combining the url = "http://mywebsite.com/" and @POST("/news_folder/search.php/") it seems to me the result would be url = "http://mywebsite.com//news_folder/search.php/"). Therefore the double "//" could be the issue?

    – Barns
    Nov 14 '18 at 18:09













  • I tried it, but it still has the same result.

    – Farid
    Nov 14 '18 at 18:23











  • Do not only Toast error messages. Use Log.e("tag", "Error ", t); this way you can find exact cause for the error. And edit the question, post error logs here.

    – Shashanth
    Nov 15 '18 at 3:51














1












1








1








i'm trying to post a request to my host with retrofit. but retrofit return null and on Failure method runs.



The problem is not hosts. I tried the request on postman and there is no problem with it.



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();
}
});


}
});}


this is my interface for retrofit:



public interface MyApi
{
@POST("/news_folder/search.php/")
Call<Fields> createTweet(@Body Fields fields);
}


url class for url variable:



public class Urlclass
{
public static final String url = "http://mywebsite.com/";
}


retrofit class:



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;
}
}


and my field:



public class Fields
{
@SerializedName("search")
public String search;
}


Which part is wrong?










share|improve this question














i'm trying to post a request to my host with retrofit. but retrofit return null and on Failure method runs.



The problem is not hosts. I tried the request on postman and there is no problem with it.



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();
}
});


}
});}


this is my interface for retrofit:



public interface MyApi
{
@POST("/news_folder/search.php/")
Call<Fields> createTweet(@Body Fields fields);
}


url class for url variable:



public class Urlclass
{
public static final String url = "http://mywebsite.com/";
}


retrofit class:



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;
}
}


and my field:



public class Fields
{
@SerializedName("search")
public String search;
}


Which part is wrong?







android retrofit






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 14 '18 at 17:13









FaridFarid

61




61













  • So, the onFailure method is being triggered? what is the cause?

    – Barns
    Nov 14 '18 at 17:36











  • Yes. The onFailure method is run. "fail it" messege and "null" showing in toast. i don,t know why.

    – Farid
    Nov 14 '18 at 17:53








  • 1





    if your are combining the url = "http://mywebsite.com/" and @POST("/news_folder/search.php/") it seems to me the result would be url = "http://mywebsite.com//news_folder/search.php/"). Therefore the double "//" could be the issue?

    – Barns
    Nov 14 '18 at 18:09













  • I tried it, but it still has the same result.

    – Farid
    Nov 14 '18 at 18:23











  • Do not only Toast error messages. Use Log.e("tag", "Error ", t); this way you can find exact cause for the error. And edit the question, post error logs here.

    – Shashanth
    Nov 15 '18 at 3:51



















  • So, the onFailure method is being triggered? what is the cause?

    – Barns
    Nov 14 '18 at 17:36











  • Yes. The onFailure method is run. "fail it" messege and "null" showing in toast. i don,t know why.

    – Farid
    Nov 14 '18 at 17:53








  • 1





    if your are combining the url = "http://mywebsite.com/" and @POST("/news_folder/search.php/") it seems to me the result would be url = "http://mywebsite.com//news_folder/search.php/"). Therefore the double "//" could be the issue?

    – Barns
    Nov 14 '18 at 18:09













  • I tried it, but it still has the same result.

    – Farid
    Nov 14 '18 at 18:23











  • Do not only Toast error messages. Use Log.e("tag", "Error ", t); this way you can find exact cause for the error. And edit the question, post error logs here.

    – Shashanth
    Nov 15 '18 at 3:51

















So, the onFailure method is being triggered? what is the cause?

– Barns
Nov 14 '18 at 17:36





So, the onFailure method is being triggered? what is the cause?

– Barns
Nov 14 '18 at 17:36













Yes. The onFailure method is run. "fail it" messege and "null" showing in toast. i don,t know why.

– Farid
Nov 14 '18 at 17:53







Yes. The onFailure method is run. "fail it" messege and "null" showing in toast. i don,t know why.

– Farid
Nov 14 '18 at 17:53






1




1





if your are combining the url = "http://mywebsite.com/" and @POST("/news_folder/search.php/") it seems to me the result would be url = "http://mywebsite.com//news_folder/search.php/"). Therefore the double "//" could be the issue?

– Barns
Nov 14 '18 at 18:09







if your are combining the url = "http://mywebsite.com/" and @POST("/news_folder/search.php/") it seems to me the result would be url = "http://mywebsite.com//news_folder/search.php/"). Therefore the double "//" could be the issue?

– Barns
Nov 14 '18 at 18:09















I tried it, but it still has the same result.

– Farid
Nov 14 '18 at 18:23





I tried it, but it still has the same result.

– Farid
Nov 14 '18 at 18:23













Do not only Toast error messages. Use Log.e("tag", "Error ", t); this way you can find exact cause for the error. And edit the question, post error logs here.

– Shashanth
Nov 15 '18 at 3:51





Do not only Toast error messages. Use Log.e("tag", "Error ", t); this way you can find exact cause for the error. And edit the question, post error logs here.

– Shashanth
Nov 15 '18 at 3:51












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305508%2fretrofit-return-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53305508%2fretrofit-return-null%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Florida Star v. B. J. F.

Error while running script in elastic search , gateway timeout

Adding quotations to stringified JSON object values