0

听说Retrofit比较简单好用。因为AQuery最初是用来下载图片的,所以和服务器做网络不是很好。我阅读了文件,但我无法理解。如果我看到一个例子,我想我可以理解。有人可以在这里举例说明如何将此代码更改为 Retofit

第一个代码是使用 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 使用 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();
            }
        }
    });
}

我只是一名学生,所以...请详细说明。谢谢你。

4

1 回答 1

0

我建议阅读以下内容:

1- 无需改装即可下载照片:

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

2-如果您想使用改造,您可以在此链接中查看示例

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

于 2017-08-26T19:05:49.500 回答