首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AQuery JSONObject空

AQuery JSONObject空
EN

Stack Overflow用户
提问于 2017-08-26 11:50:24
回答 1查看 128关注 0票数 0

这是我试图使用的完整的片段代码。我用AQuery得到了json。但我有个问题。JsonObject,JsonObject。总是空的。我试过“邮递员”的网址,它还了我所需要的json。问题是1,我将JSONObject更改为String,并得到了文件。怎么可能。2.我听说了改造的事。我读了所有的文档,但是我不能理解..According给那些使用Retrofit的人,他们说Retrofit比AQuery好。如何将此代码更改为Retrofit? 3.这是一个AQueryproblem还是我的代码的问题?

谢谢。

代码语言:javascript
复制
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;
}
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-08-26 12:14:48

您必须将此添加到应用程序build.gradle中。

代码语言:javascript
复制
compile 'com.squareup.retrofit2:retrofit:2.3.0'
compile "com.squareup.retrofit2:converter-gson:2.3.0"

接下来创建一个像这样的Webservice.class,(您不需要授权令牌,只有在有令牌关联调用的情况下)

代码语言:javascript
复制
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实例

代码语言:javascript
复制
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);

最后把它叫做

代码语言:javascript
复制
Response<List<State>> response = services.getStates("Bearer "+token).execute();

在应用程序体系结构方面,您可以按照Google,https://developer.android.com/topic/libraries/architecture/guide.html的指示

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45895031

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档