0
votes

Im trying to get onResponse() to execute in my Callback. for some reason onFailure keeps getting called even though the server is returning a 200 code and I'm also getting my data in its entirety.

I'm not sure what must be done for onResponse to execute properly so I can parse the JSON and add the objects to a List View.

relevant fragment:-

public class ActivePostFragment extends ListFragment  implements AdapterView.OnItemClickListener, OnClickListener{
    private ProgressBar progress;
    private Button btn_newPost;
    private RecyclerView recyclerView;
    private MyItemRecyclerViewAdapter mAdapter;
 public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_activepost,container,false);
        initViews(view);
        int uIdValue = getArguments().getInt("userID");
        Log.d(Constants.TAG, String.valueOf(uIdValue));
        progress.setVisibility(View.VISIBLE);
        loadPostsProcess(uIdValue);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
        recyclerView.setLayoutManager(mLayoutManager);
        recyclerView.setItemAnimator(new DefaultItemAnimator());
        return view;
    }
    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
    }
    @Override
    public void onClick(View v){
        switch (v.getId()){
            case R.id.newPost:
                goToNewPost();
                break;
        }
    }
    private void initViews(View view){
        btn_newPost = (Button) view.findViewById(R.id.newPost);
        recyclerView = (android.support.v7.widget.RecyclerView) view.findViewById(R.id.recyclerView);
        btn_newPost.setOnClickListener(this);
        progress = (ProgressBar)view.findViewById(R.id.activePostProgress);
    }
    private void loadPostsProcess(int uIdValue) {
        HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
        interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
        OkHttpClient client = new OkHttpClient.Builder().addInterceptor(interceptor).build();
        Gson gson = new GsonBuilder()
                .setLenient()
                .create();
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(Constants.BASE_URL)
                .client(client)
                .addConverterFactory(GsonConverterFactory.create(gson))
                .build();
        RequestInterface requestInterface = retrofit.create(RequestInterface.class);
        User user = new User();
        ServerRequest request = new ServerRequest();
        request.setOperation(Constants.RETRIEVE_ALL_OPERATION);
        request.setUser(user);
        final Call<PostsServerResponse> response = requestInterface.postsByUser(uIdValue);
            response.enqueue(new Callback<PostsServerResponse>() {
                @Override
                public void onResponse(Call<PostsServerResponse> call, retrofit2.Response<PostsServerResponse> response) {
                    PostsServerResponse resp = response.body();
                    String jsonServerPosts = resp.getPosts();

                    Type listType = new TypeToken<ArrayList<Post>>(){}.getType();
                    final ArrayList<Post> jsonArrayList = new Gson().fromJson(jsonServerPosts, listType);
                    Log.d(Constants.TAG, jsonArrayList.toString());

                    RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity().getApplicationContext());
                    recyclerView.setLayoutManager(mLayoutManager);
                    recyclerView.setItemAnimator(new DefaultItemAnimator());
                    recyclerView.setAdapter(new MyItemRecyclerViewAdapter(jsonArrayList, getActivity()));
                    progress.setVisibility(View.INVISIBLE);
                }
                @Override
                public void onFailure(Call<PostsServerResponse> call, Throwable t) {
                    Log.d(Constants.TAG, "Retrofit: onResponse not called, onFailure called instead... ");
                    Log.d(Constants.TAG, t.getStackTrace().toString());
                    Log.d(Constants.TAG, t.getMessage());
                    Snackbar.make(getView(), t.getLocalizedMessage(), Snackbar.LENGTH_INDEFINITE).show();
                    progress.setVisibility(View.INVISIBLE);
                }
            });
    }
    private void goToViewPost(){
    }
    private void goToNewPost(){
        Fragment newPost = new NewPostFragment();
        FragmentTransaction ft = getFragmentManager()
                .beginTransaction();
        ft.replace(R.id.fragment_frame, newPost);
        ft.commit();
    }
}

request interface:-

public interface RequestInterface {

@POST("index.php/")
Call<LoginServerResponse> operation(@Body ServerRequest request);

@GET("activePosts.php/")
Call<PostsServerResponse> postsByUser(@Query("uid") int userID);}

ServerResponse class:

public class PostsServerResponse {
    private String result;
    private String message;
    private String posts;
    public String getResult() {
        return result;
    }
    public String getMessage() {
        return message;
    }
    public String getPosts() {
        return posts;
    }
}

php code:-

require_once 'Functions.php';
$fun = new Functions();

if ($_SERVER['REQUEST_METHOD'] == 'GET'){
  $queryUserID = htmlspecialchars($_GET["uid"]);
  echo 'url query ID is: '.$queryUserID.'. ';
  echo $fun -> getPosts($queryUserID);

functions.php:

public function getPosts($queryUserID){
      $db = $this -> db;

  if ($db -> postsExist($queryUserID)){
    $result = $db -> getUserPosts($queryUserID);
    $response["result"] = "Success. ";
    $response["message"] = "Posts found. ";
    // might need to handle the formatting of the '$posts' object if there are multiple lines
    $response["posts"] = $result;
    return json_encode($response);
  }
  else{
    echo 'no active posts for this user ID. ';
  }
}

DBOperations:

public function getUserPosts($queryUserID){
    $sql = 'SELECT title, content FROM users JOIN post
                ON post.userID = users.sno WHERE sno = ?';
    $queryStmt = $this -> conn -> prepare($sql);
    if ($queryStmt -> bindParam(1, $queryUserID, PDO::PARAM_INT)) {
        echo '<br> userID: '.$queryUserID.' URL parameter bound successfully. </br>';
        if($queryStmt -> execute() ){
            echo '<br> query execution successful. there are rows. </br>';
            $resultSet = $queryStmt -> fetchAll(PDO::FETCH_ASSOC);

            return $resultSet;
        }
    }
}

this is my response from server:

url query ID is: 3. <br> userID: 3 URL parameter bound successfully. </br><br> query execution successful. there are rows. </br>{"posts":[{"title":"test title 1","content":"hey this is the content for test title 1"},{"title":"test title 2","content":"hey this is the content for the second post!"}]}

this is stack trace and exception:

[Ljava.lang.StackTraceElement;@efa7528 java.lang.IllegalStateException: Expected BEGIN_OBJECT but was STRING at line 1 column 1 path $

if I haven't included all relevant information, let me know

1
While you may be generating JSON, that is not what your server appears to be returning, based on the error message. The server is returning something that does not begin with a {, apparently. You might wish to make the request through your OkHttpClient, instead of through Retrofit, and see exactly what the server is serving you. - CommonsWare
try dropping your two echo lines from getUserPosts. they are causing the response to get additional String messages ahead of your json. - kandroidj
you're correct in suggesting the echo line reductions. thank you, now it gives me "Expected a string but was BEGIN_ARRAY at line 1 column 58 path $.posts". is this saying its expecting a string where all my posts objects are? if so how do I make them a string when the post objects are stored in an array in the php? - user5932019

1 Answers

0
votes

Retrofit is expecting an object but it's receiving an string.

Replace echo 'url query ID is: '.$queryUserID.'. '; echo $fun -> getPosts($queryUserID);

With

echo getPosts($queryUserID);