title: Android PHP JSON parsing giving 404 error but fetching data
body: <p>I have a problem which is quite confusing for me. So, I am parsing data from mysql to my android device. The thing is from the server end, I am getting a 404 error but data is getting fetched successfully and populated in my listview. This is quite disadvantageous as I have to bypass the code </p>
  
  <pre><code>StatusLine responseStatus = response.getStatusLine();
    if (responseStatus.getStatusCode() != 200) {}
  </code></pre>
  
  <p>Which is responsible for detecting an actual server error. The link I call from the android end to fetch the json data is <code>http://applink.skibzy.com/1/index.php?action=fetchdata</code> and when I call the url from a browser, the json encoded data is displayed and a http status code 404 is registered on the server end. Kindly help me solve this awkward yet confusing issue. The respective files involved are FetchBlogPostData.java, MainActivity.java and index.php</p>
  
  <p><strong>index.php</strong></p>
  
  <pre><code>&lt;?php
  require('db.php');
  $action = $_GET['action'];
  
  if ($action == 'fetchdata') {
  $query = mysql_query('SELECT * FROM posts ORDER BY date DESC');
  if (mysql_num_rows($query) &gt;= '1') {
      $a = array();
      while ($b = mysql_fetch_array($query)) {
          $post = array();
          $post["title"] = $b['post_title'];
          //$post["who"] = DB_USER;
          array_push($a, $post);  
      }
      echo json_encode($a);
  }
  }
  ?&gt;
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends ListActivity implements FetchDataListener {
  
  Constants constant;
  Utils utils;
  
  private ActionBarActivity action;
  private ProgressBar dialog;
  public TextView post_id;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      constant = new Constants();
      utils = new Utils(this);
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      dialog = (ProgressBar) findViewById(R.id.progressBar);
  
      initView();
  }
  
  private void initView() {
  // show progress dialog
      dialog.setVisibility(View.VISIBLE);
      String url = "http://applink.skibzy.com/1/index.php?action=fetchdata";
      FetchBlogPostData task = new FetchBlogPostData(this);
      task.execute(url);
      //getListView().setOnClickListener(this);
  }
  
  @Override
  public void onFetchComplete(List&lt;BlogPostValue&gt; data) {
      // dismiss the progress dialog
      if(dialog != null) dialog.setVisibility(View.GONE);
      post_id = (TextView) findViewById(R.id.postID);
      // create new adapter
      BlogPostAdapter adapter = new BlogPostAdapter(this, data);
      // set the adapter to list
      setListAdapter(adapter);
  }
  @Override
  public void onFetchFailure(String msg) {
      // dismiss the progress dialog
      if(dialog != null) dialog.setVisibility(View.GONE);
  
      Bundle dataBundle = new Bundle();
      dataBundle.putString("error", msg);
      dataBundle.putString(constant.PREVIOUS_ACTIVITY, constant.THE_MAIN_ACTIVITY);
      Intent intent = new Intent(getApplicationContext(), ErrorActivity.class);
      intent.putExtras(dataBundle);
      startActivity(intent);
      finish();
  }
  }
  </code></pre>
  
  <p><strong>FetchBlogPostData.java</strong></p>
  
  <pre><code>public class FetchBlogPostData extends AsyncTask&lt;String, Void, String&gt; {
  private final FetchDataListener listener;
  private String msg;
  public FetchBlogPostData(FetchDataListener listener) {
      this.listener = listener;
  }
  @Override
  protected String doInBackground(String... params) {
      if(params == null) return null;
          // get url from params
          String url = params[0];
          try {
              HttpClient client = new DefaultHttpClient();
              HttpGet httpget = new HttpGet(url);
  
              HttpResponse response = client.execute(httpget);
  
              HttpEntity entity = response.getEntity();
              if(entity == null) {
                  msg = "Try Again";
                  return null;
              }
  
              InputStream is = entity.getContent();
              return streamToString(is);
          }
          catch(IOException e){
              msg = "No Internet Connection";
          }
      return null;
  }
  @Override
  protected void onPostExecute(String sJson) {
      if (sJson == null) {
          if (listener != null) listener.onFetchFailure(msg);
          return;
      }
      try {
          // convert json string to json array
          JSONArray aJson = new JSONArray(sJson);
          // create apps list
          List&lt;BlogPostValue&gt; apps = new ArrayList&lt;BlogPostValue&gt;();
          for (int i = 0; i &lt; aJson.length(); i++) {
              JSONObject json = aJson.getJSONObject(i);
              BlogPostValue app = new BlogPostValue();
              app.setID(json.getString("id"));
              app.setTitle(json.getString("title"));
              app.setExerpt(json.getString("exerpt"));
              app.setCreated(json.getString("created"));
              app.setTotalComments(json.getString("comments"));
              app.setCommentStatus(json.getString("comment_status"));
              app.setIcon(json.getString("thumbnail"));
              // add the app to apps list
              apps.add(app);
          }
          //notify the activity that fetch data has been complete
          if (listener != null) listener.onFetchComplete(apps);
      } catch (JSONException e) {
          msg = "Try Again";
          if (listener != null) listener.onFetchFailure(msg);
              return;
      }
  }
  
  /**
   * This function will convert response stream into json string
   * @param is respons string
   * @return json string
   * @throws IOException
   */
  public String streamToString(final InputStream is) throws IOException{
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      StringBuilder sb = new StringBuilder();
      String line = null;
      try {
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
      }
      catch (IOException e) {
          throw e;
      }
      finally {
          try {
              is.close();
          }
          catch (IOException e) {
              throw e;
          }
      }
      return sb.toString();
  }
  }
  </code></pre>