这是我试图使用的完整的片段代码。我用AQuery得到了json。但我有个问题。JsonObject,JsonObject。总是空的。我试过“邮递员”的网址,它还了我所需要的json。问题是1,我将JSONObject更改为String,并得到了文件。怎么可能。2.我听说了改造的事。我读了所有的文档,但是我不能理解..According给那些使用Retrofit的人,他们说Retrofit比AQuery好。如何将此代码更改为Retrofit? 3.这是一个AQueryproblem还是我的代码的问题?
谢谢。
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;
}
}发布于 2017-08-26 12:14:48
您必须将此添加到应用程序build.gradle中。
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile "com.squareup.retrofit2:converter-gson:2.3.0"接下来创建一个像这样的Webservice.class,(您不需要授权令牌,只有在有令牌关联调用的情况下)
public interface WebServices {
@GET("metadata/countries")
Call<List<Country>> getCountries(@Header("Authorization") String authorization);
@GET("metadata/states") ;; In your case should @POST("SendPastQuestion")
Call<List<State>> getStates(@Header("Authorization") String authorization);
}然后您需要创建一个Retrofit实例
Webservice service = new Retrofit.Builder()
.baseUrl(Codes.V1.getDescription()) ;; In your case should be "http://192.168.0.21:3000/"
.addConverterFactory(GsonConverterFactory.create())
.build()
.create(WebServices.class);最后把它叫做
Response<List<State>> response = services.getStates("Bearer "+token).execute();在应用程序体系结构方面,您可以按照Google,https://developer.android.com/topic/libraries/architecture/guide.html的指示
https://stackoverflow.com/questions/45895031
复制相似问题