0

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.

dogyhbin2
  • 25
  • 6

1 Answers1

0

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

Hazem
  • 342
  • 1
  • 5
  • 19
  • Thank you. Is there codes to post JSON?? There are example codes to download files or images – dogyhbin2 Aug 26 '17 at 19:09
  • Do you want post JSON for image or another thing? In java classes when you put implement Serializable you can directly recieve JSON results into objects by retrofit request – Hazem Aug 26 '17 at 19:22
  • I fell that I am a stupid................................I can't understand What you are saying................ – dogyhbin2 Aug 26 '17 at 19:31
  • No it is normal you can ask any thing – Hazem Aug 26 '17 at 19:34
  • public interface WebService { @POST("/SendPastQuestion") Call>listRepos(@FormUrlEncoded("pastquestion")JSONObject pastquestion); } – dogyhbin2 Aug 26 '17 at 19:39
  • at this code @FormUrlEncoded("pastquestion") What do I have to replace it. I makes an error... – dogyhbin2 Aug 26 '17 at 19:40
  • I can't understand a part of your code any way why you don't use http://square.github.io/picasso/ for download photos It is more simple – Hazem Aug 26 '17 at 19:48