我听说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;
}
}第二个代码是使用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();
}
}
});
}我只是个学生所以。请详细解释一下。谢谢。
发布于 2017-08-27 03:05:50
我建议阅读以下内容:
1-无需改造即可下载照片:
https://stackoverflow.com/a/25463200/7709267
2-如果您想使用改进,您可以在此链接中查看示例
https://futurestud.io/tutorials/retrofit-2-how-to-download-files-from-server
https://stackoverflow.com/questions/45898659
复制相似问题