Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/templates/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 改装中未调用onResponse方法?_Java_Android_Json_Android Recyclerview_Retrofit - Fatal编程技术网

Java 改装中未调用onResponse方法?

Java 改装中未调用onResponse方法?,java,android,json,android-recyclerview,retrofit,Java,Android,Json,Android Recyclerview,Retrofit,我正在使用改装创建一个应用程序。我从URL的JSON字符串中获取数据,并使用改型在循环视图中填充它 我的问题 我的问题是,当应用程序执行时,它会显示以下错误: E/RecyclerView:未连接适配器;跳过布局E/RecyclerView: 无适配器连接;跳过布局D/错误: java.lang.IllegalStateException:应为BEGIN\u对象,但为 从第1行第2列路径开始\u数组$ 我的支票 检查了URL 已使用浏览器检查URL。。它成功返回json 我努力想解决这个问题,但

我正在使用改装创建一个应用程序。我从URL的JSON字符串中获取数据,并使用改型在循环视图中填充它

我的问题

我的问题是,当应用程序执行时,它会显示以下错误:

E/RecyclerView:未连接适配器;跳过布局E/RecyclerView: 无适配器连接;跳过布局D/错误: java.lang.IllegalStateException:应为BEGIN\u对象,但为 从第1行第2列路径开始\u数组$

我的支票

检查了URL 已使用浏览器检查URL。。它成功返回json 我努力想解决这个问题,但没有解决办法

我手动运行json url并成功返回json数据。但在程序上,它返回错误

JSON数据示例

用于JSON的Post.javapojo

DataAdapter.java

RequestInterface.java

maintactivity.java

public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ArrayList<Post> data;
    private DataAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }
    private void initViews(){
        recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        loadJSON();
    }
    private void loadJSON(){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://demo.cogzideltemplates.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<JSONResponse> call = request.getJSON();
        System.out.println("enter the url: "+call.request().url());

        call.enqueue(new Callback<JSONResponse>() {

            @Override
            public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {

                JSONResponse jsonResponse = response.body();

                data = new ArrayList<>(Arrays.asList(jsonResponse.getPost()));
                adapter = new DataAdapter(data);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<JSONResponse> call, Throwable t) {
                Log.d("Error",t.getMessage());

            }
        });
    }
}

这些是我的课。请帮我解决这个问题

将JSON数据样本更改为JSON对象 像这样

{
[
  {
    "user_id": "59e4897140d1666c6e8b4567",
    "post_id": "59eeb10d40d16686168b4567",
    "post_url": "http://demo.cogzideltemplates.com/anan/posts/images/22f3fb0b974be4968118a410e0ad48f7.jpg",
    "post_type": "image",
    "caption": "vvnnk",
    "created": 1508815117,
    "time_diff": "1 hours ago",
    "first_name": null,
    "last_name": null,
    "user_name": null,
    "email": null,
    "profile_pic": "http://demo.cogzideltemplates.com/anan/images/users/no_avatar.jpg",
    "category": "New release"
  },
  {
    "user_id": "59e4897140d1666c6e8b4567",
    "post_id": "59ee306940d166697f8b4567",
    "post_url": "http://demo.cogzideltemplates.com/anan/posts/images/8f81bd77e60e596cf458d42a743897f8.jpg",
    "post_type": "image",
    "caption": "cutyy",
    "created": 1508782185,
    "time_diff": "10 hours ago",
    "first_name": null,
    "last_name": null,
    "user_name": null,
    "email": null,
    "profile_pic": "http://demo.cogzideltemplates.com/anan/images/users/no_avatar.jpg",
    "category": "Super Star"
  }
]}
并更改JSONResponse.java

像这样

{
[
  {
    "user_id": "59e4897140d1666c6e8b4567",
    "post_id": "59eeb10d40d16686168b4567",
    "post_url": "http://demo.cogzideltemplates.com/anan/posts/images/22f3fb0b974be4968118a410e0ad48f7.jpg",
    "post_type": "image",
    "caption": "vvnnk",
    "created": 1508815117,
    "time_diff": "1 hours ago",
    "first_name": null,
    "last_name": null,
    "user_name": null,
    "email": null,
    "profile_pic": "http://demo.cogzideltemplates.com/anan/images/users/no_avatar.jpg",
    "category": "New release"
  },
  {
    "user_id": "59e4897140d1666c6e8b4567",
    "post_id": "59ee306940d166697f8b4567",
    "post_url": "http://demo.cogzideltemplates.com/anan/posts/images/8f81bd77e60e596cf458d42a743897f8.jpg",
    "post_type": "image",
    "caption": "cutyy",
    "created": 1508782185,
    "time_diff": "10 hours ago",
    "first_name": null,
    "last_name": null,
    "user_name": null,
    "email": null,
    "profile_pic": "http://demo.cogzideltemplates.com/anan/images/users/no_avatar.jpg",
    "category": "Super Star"
  }
]}
public class JSONResponse {
private List<Post> post;

public List<Post> getPost() {
    return post;
}
}


JSONResponse的可能重复项是object,而api返回的JSONArray不是JSONObject您没有在布局管理器中指定它是垂直、水平还是网格etc@JRamesh请explain@RageshDAntony如果您想使用上述代码解析响应,那么响应将如下{[{},{}]}谢谢您的回答。。但遗憾的是,我无法更改json字符串,因为它直接从internet获取。请您在不更改JSON的情况下给出解决方案@GirishI解决了我的问题。。我只是把所有的事件都改成了,它成功了。。谢谢大家
import retrofit2.Call;
import retrofit2.http.GET;

public interface RequestInterface {

    @GET("anan/mobile/posts/viewall_Posts")
    Call<JSONResponse> getJSON();
}
public class MainActivity extends AppCompatActivity {

    private RecyclerView recyclerView;
    private ArrayList<Post> data;
    private DataAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initViews();
    }
    private void initViews(){
        recyclerView = (RecyclerView)findViewById(R.id.card_recycler_view);
        recyclerView.setHasFixedSize(true);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
        recyclerView.setLayoutManager(layoutManager);
        loadJSON();
    }
    private void loadJSON(){

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://demo.cogzideltemplates.com")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        RequestInterface request = retrofit.create(RequestInterface.class);
        Call<JSONResponse> call = request.getJSON();
        System.out.println("enter the url: "+call.request().url());

        call.enqueue(new Callback<JSONResponse>() {

            @Override
            public void onResponse(Call<JSONResponse> call, Response<JSONResponse> response) {

                JSONResponse jsonResponse = response.body();

                data = new ArrayList<>(Arrays.asList(jsonResponse.getPost()));
                adapter = new DataAdapter(data);
                recyclerView.setAdapter(adapter);
            }

            @Override
            public void onFailure(Call<JSONResponse> call, Throwable t) {
                Log.d("Error",t.getMessage());

            }
        });
    }
}
{
[
  {
    "user_id": "59e4897140d1666c6e8b4567",
    "post_id": "59eeb10d40d16686168b4567",
    "post_url": "http://demo.cogzideltemplates.com/anan/posts/images/22f3fb0b974be4968118a410e0ad48f7.jpg",
    "post_type": "image",
    "caption": "vvnnk",
    "created": 1508815117,
    "time_diff": "1 hours ago",
    "first_name": null,
    "last_name": null,
    "user_name": null,
    "email": null,
    "profile_pic": "http://demo.cogzideltemplates.com/anan/images/users/no_avatar.jpg",
    "category": "New release"
  },
  {
    "user_id": "59e4897140d1666c6e8b4567",
    "post_id": "59ee306940d166697f8b4567",
    "post_url": "http://demo.cogzideltemplates.com/anan/posts/images/8f81bd77e60e596cf458d42a743897f8.jpg",
    "post_type": "image",
    "caption": "cutyy",
    "created": 1508782185,
    "time_diff": "10 hours ago",
    "first_name": null,
    "last_name": null,
    "user_name": null,
    "email": null,
    "profile_pic": "http://demo.cogzideltemplates.com/anan/images/users/no_avatar.jpg",
    "category": "Super Star"
  }
]}
public class JSONResponse {
private List<Post> post;

public List<Post> getPost() {
    return post;
}