dogyhbin2
dogyhbin2

Reputation: 25

AQuery to Retofit

I heard that Retrofit is more simple and useful. because AQuery was first made to download images so It isn't that good to do network with the server. I read the documents but I couldn't understand it. If I see an example, I think I can understand. could someone here give a example how to change this code to Retofit

First code is getting the values from the server using url.

public class PastQuestionFragment extends Fragment {
AQuery aq = new AQuery(getActivity());
String postUrl = "http://192.168.0.21:3000/SendPastQuestion";

TextView pastQuestion;

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup 
container, Bundle savedInstanceState) {
    ViewGroup rootView = (ViewGroup) 
inflater.inflate(R.layout.fragment_pastquestion, container, false);
    pastQuestion = (TextView) rootView.findViewById(R.id.pastquestion);


    aq.ajax(postUrl, JSONObject.class, new AjaxCallback<JSONObject>() {
        @Override
        public void callback(String url, JSONObject json, AjaxStatus status) 
{
            if (json != null) {
                Log.d("debug", "json is not null");
                try {
                    pastQuestion.setText(json.getString("pastquestion"));
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.d("debug", "json is null");
            }
        }
    });
    return rootView;
}
}

Second Code is sending values using post.

public void postSignUpData(String name, String id, String password, String email) {
    String postUrl = "http://192.168.0.21:3000/SignUp";
    Map<String, Object> params = new HashMap<>();
    params.put("name", name);
    params.put("id", id);
    params.put("password", password);
    params.put("email", email);

    aq.ajax(postUrl, params, String.class, new AjaxCallback<String>() {
        @Override
        public void callback(String url, String json, AjaxStatus status) {
            if (json != null) {
                Toast.makeText(aq.getContext(), "Status1 : " + status.getCode() + " + " + json.toString(), Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(aq.getContext(), "Status2 : " + status.getCode(), Toast.LENGTH_SHORT).show();
            }
        }
    });
}

I am just a student so... please explain details please. Thank you.

Upvotes: 0

Views: 99

Answers (1)

Hazem
Hazem

Reputation: 352

I would recommend reading the follow:

1- For download photos without retrofit:

https://stackoverflow.com/a/25463200/7709267

2- If you want use retrofit you can see examples in this link

https://futurestud.io/tutorials/retrofit-2-how-to-download-files-from-server

Upvotes: 0

Related Questions