0

I want to fetch JSON string array from server using Retrofit. The JSON array is like this.

["A","B","C"]

Here is the code I made .

MainActivity.class

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    String url="http://proverbs-app.antjan.us";

    RestAdapter restAdapter;        
    restAdapter = new RestAdapter.Builder()
    .setEndpoint(url)
    .build(); 

    Service service=restAdapter.create(Service.class);
    service.get(new Callback<List<String>>(){


        @Override

        public void failure(RetrofitError error) {
            Toast.makeText(getApplicationContext(), "Error", Toast.LENGTH_LONG).show();
         }

        @Override
        public void success(List<String> arg0, Response arg1) {
            Toast.makeText(getApplicationContext(), arg0.toString(), Toast.LENGTH_LONG).show();

        }
         });    
}

}

Here is the Service.class

public interface Service {
@GET("/")
public void get(Callback<List<String>> callback);

}

It shows only "Error" Toast.

Am I something wrong with my codes. Please help me to fix it. I'm a beginner. So,please answer me in detail because I don't know much about it. Thanks in advance.

4
  • What kind of error are you facing? Commented May 12, 2015 at 14:35
  • post your logcat error..we cannot predict without logcat error Commented May 12, 2015 at 14:38
  • Please do not change the question when you face a new problem. Other users might run into the original issue, and answers provided below are now no longer related to the question. Commented May 12, 2015 at 15:08
  • @LinLin Enable logging for Retrofit and check the log for possible error new RestAdapter.Builder().setLogLevel(RestAdapter.LogLevel.FULL) Commented May 12, 2015 at 15:10

2 Answers 2

1

You need to specify relative path for URL in the interface.

@GET("/")
public void get(Callback<List<String>> callback);
Sign up to request clarification or add additional context in comments.

5 Comments

Yes , sir . I tested by changing it but still show the same logcat output I've shown.
public interface Service { @GET("/") public void get(Callback<List<String>> callback); }
@LinLin Since you are getting NoClassDefFoundError try kevin's solution and also add thes proguard-square-retrofit.pro lines in your Proguard file.
@b1izzardNow I can fix crashing. But I got an "Error" Toast while running app. Can u check where I was wrong,sir? I accidentally edited the question.I'm so sorry ,sir.
@LinLin Had you enabled retrofit logging, as I mentioned in the above comment?
1

It looks like you're missing Gson. Add this line to your dependencies block in build.gradle:

compile 'com.google.code.gson:gson:2.3.1'

5 Comments

Sir,I'm now using Eclipse . I added Retrofit.jar to build path.
Then you should download the Gson jar and add that one as well. You can get it here: search.maven.org/…
Yes , sir .It fix app crashing. But showing a toast "Error". Can you check my codes ,sir?
You would have to paste the content of the RetroFit error before anyone would be able to see what is wrong. If you can't find the problem yourself, you should create a new question, because it is not directly related to this one.
I'm sorry sir.I'm a newcomer and I didn't notice that. Can you also check what am I wrong right now , Sir?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.