Android 我可以在主线程中发出Json截击请求吗?

Android 我可以在主线程中发出Json截击请求吗?,android,json,multithreading,Android,Json,Multithreading,我需要发出一个JSON请求并根据其响应设置UI,因此最好在同一线程中的onCreate中创建它 说到未来,我不能可靠地使用它,或者我不知道如何使用它 RequestFuture<JSONObject> future = RequestFuture.newFuture(); JsonObjectRequest request = new JsonObjectRequest(URL, null, future, future); requestQueue.add(request); t

我需要发出一个JSON请求并根据其响应设置UI,因此最好在同一线程中的onCreate中创建它

说到未来,我不能可靠地使用它,或者我不知道如何使用它

RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(URL, null, future, future);
requestQueue.add(request);

try {
  JSONObject response = future.get();
  count = response.getIn("int");
} catch (InterruptedException e) {
} catch (ExecutionException e) {
}catch (JSONException e) {
            Toast.makeText(getActivity(), e.toString(), Toast.LENGTH_LONG).show();
        }
RequestFuture=RequestFuture.newFuture();
JsonObjectRequest=新的JsonObjectRequest(URL,null,future,future);
添加(请求);
试一试{
JSONObject response=future.get();
count=response.getIn(“int”);
}捕捉(中断异常e){
}捕获(执行例外){
}捕获(JSONException e){
Toast.makeText(getActivity(),e.toString(),Toast.LENGTH_LONG).show();
}
我试过这个,但不管用

如果有人知道如何从JSONObject获取数据,请给出一个示例,在获取数据之前什么都不做,然后使用该值来设置一些视图。

由于代码中有getActivity(),我打赌您是在一个片段中。 要在UI线程上执行某些操作,您可以调用如下内容

    getActivity().runOnUiThread(new Runnable() {
        @Override
        public void run() {
            // your UI-related code goes here    
        }
    });

使用截击,当成功时您有响应侦听器,当服务器有一个带有错误代码(500401)的响应时,您有一个错误侦听器。 因此,您可以创建一个JSONObjectRequest,如下代码行所示:

    JSONObjectRequest jsObjRequest = new JsonArrayRequest
            (REQUEST_URL, new Response.Listener<JSONObjectRequest>() {

                @Override
                public void onResponse(JSONObjectRequest response) {
                    // this code will be executed when response comes and you can set your views, the way is depending where you are (fragment, activity, etc), you can do runOnUiThread
                    Log.d("Hello", "You can do whatever you want wen data is goten");
            }, new Response.ErrorListener() {

                @Override
                public void onErrorResponse(VolleyError error) {
                    Log.d("Error","You can show an error dialog here ");
                }
            });
JSONObjectRequest jsObjRequest=new-JsonArrayRequest
(REQUEST_URL,new Response.Listener()){
@凌驾
公共void onResponse(JSONObjectRequest-response){
//这段代码将在响应到来时执行,您可以设置视图,方式取决于您所在的位置(片段、活动等),您可以执行runOnUiThread
Log.d(“你好”,“你可以做任何你想做的事情,因为数据已被获取”);
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
d(“错误”,“您可以在此处显示错误对话框”);
}
});

您必须将上面创建的对象添加到HTTQueue中,并将在该对象执行时执行。希望它能有所帮助!

如果您使用的是截击,那么您可以使用侦听器。您也可以参考此内容。在onResponse或onError中,您可以进行所需的UI更改

JsonArrayRequest request = new JsonArrayRequest(url, new Response.Listener<JSONArray>() {

  @Override
  public void onResponse(JSONArray response) {


  }
}, new Response.ErrorListener() {

  @Override
  public void onErrorResponse(VolleyError error) {


    }

  }
});
JsonArrayRequest=newjsonarrayrequest(url,new Response.Listener()){
@凌驾
公共void onResponse(JSONArray响应){
}
},new Response.ErrorListener(){
@凌驾
公共无效onErrorResponse(截击错误){
}
}
});

thx获取评论,但如果可以,请写下答案并举例说明。)如果你放-,请告诉我原因,以后不要重复?thxok,我试试看。谢谢。如果我想让你参加这个活动,它是这样的?this.runOnUiThread(new Runnable(){@Override public void run(){//你的UI相关代码在这里}});或者我该怎么做呢?我想它会和这个一起工作。不管怎么说,如果没有这个,给runOnUiThread打电话,我可以看到更好的答案。使用volley的人建议使用库提供的API,我想说的是,使用官方的volley方式。但是JsonArrayRequest和JsonObjectRequest运行它自己的线程,我尝试过,在这种情况下不起作用。但是如果我不想从请求中获取值,我可以在一些空的exeption中运行,因为它在不同的线程中运行。