title: How to return data from php to eclipse?
body: <p>Hi, I am using php to get the data from mysql database and upon successful receiving of the data I call a new activity where I need to print the data.</p>
  
  <p>This is my code:</p>
  
  <pre><code>public class RecoProd extends Activity {
      EditText pd;
      TextView error;
  
      ArrayList&lt;NameValuePair&gt; postParameters;
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.recomain);
          pd = (EditText) findViewById(R.id.name);
  
          error = (TextView) findViewById(R.id.myTableLayout);
  
          pd.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  postParameters = new ArrayList&lt;NameValuePair&gt;();
                  postParameters.add(new BasicNameValuePair("product", pd
                          .getText().toString()));
  
  
                  // String valid = "1";
  
                  DownloadWebPageTask dw = new DownloadWebPageTask();
                  dw.execute("");
  
              }
          });
      }
  
      private class DownloadWebPageTask extends AsyncTask&lt;String, Void, String&gt; {
          @Override
          protected String doInBackground(String... urls) {
              String response = null;
              for (String url : urls) {
                  try {
                      response = CustomHttpClient.executeHttpPost("http://10.0.2.2/abc/check2.php", postParameters);  
                      String res = response.toString();
                      // res = res.trim();
                      res = res.replaceAll("\\s+", "");
                      // error.setText(res);
                      if(res.equals("1")){     ***&lt;&lt;--------------I think the error is here***
                          Intent loginintent = new Intent(RecoProd.this, RecoProd1.class);
                          startActivity(loginintent);
                      }
                      else
                          error.setText("Sorry!! ");
  
  
  
  
  
                  } catch (Exception e) {
  
  
                  }
              }
              return response;
          }
  
          @Override
          protected void onPostExecute(String result) {
          }
      }
  }
  </code></pre>
  
  <p>This is my php code:</p>
  
  <pre><code>&lt;?php
  $pd=$_POST['name'];
  
  //connect to the db
  $user = "root";
  $pswd = "";
  $db = "mylogin";
  $host = "localhost";
  $conn = mysql_connect($host, $user, $pswd);
  mysql_select_db($db);
  
  $query = "SELECT * FROM recoprod  ";
  $result = mysql_query("SELECT * FROM recoprod");
  while($row = mysql_fetch_array($result))
    {
    $output[]=$row;
    }
  /*
  $result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
  //this is where the actual verification happens  
  if(mysql_num_rows($result) &gt; 0)  
  echo 1;  // for correct login response  
  else  
  echo 0; // for incorrect login response */
  ?&gt;
  </code></pre>
  
  <p>I am getting a force close when I execute it.</p>
  
  <pre><code>EDITED CODE
  
  
  
  
  
  package com.androidhive.googleplacesandmaps;
  
  import java.lang.reflect.Constructor;
  import java.lang.reflect.Method;
  import java.util.ArrayList;
  import org.apache.http.NameValuePair;
  import org.apache.http.message.BasicNameValuePair;
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import android.app.Activity;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.util.Log;
  import android.view.View;
  import android.widget.EditText;
  import android.widget.TextView;
  public class RecoProd extends Activity {
      EditText pd;
      TextView error;
  
      ArrayList&lt;NameValuePair&gt; postParameters;
      /** Called when the activity is first created. */
      @Override
      public void onCreate(Bundle savedInstanceState) {
  
          try {
              Class&lt;?&gt; strictModeClass = Class.forName("android.os.StrictMode", true, Thread.currentThread()
                      .getContextClassLoader());
  
              Class&lt;?&gt; threadPolicyClass = Class.forName("android.os.StrictMode$ThreadPolicy", true, Thread
                      .currentThread().getContextClassLoader());
  
              Class&lt;?&gt; threadPolicyBuilderClass = Class.forName("android.os.StrictMode$ThreadPolicy$Builder", true,
                      Thread.currentThread().getContextClassLoader());
  
              Method setThreadPolicyMethod = strictModeClass.getMethod("setThreadPolicy", threadPolicyClass);
  
              Method detectAllMethod = threadPolicyBuilderClass.getMethod("detectAll");
              Method penaltyMethod = threadPolicyBuilderClass.getMethod("penaltyLog");
              Method buildMethod = threadPolicyBuilderClass.getMethod("build");
  
              Constructor&lt;?&gt; threadPolicyBuilderConstructor = threadPolicyBuilderClass.getConstructor();
              Object threadPolicyBuilderObject = threadPolicyBuilderConstructor.newInstance();
  
              Object obj = detectAllMethod.invoke(threadPolicyBuilderObject);
  
              obj = penaltyMethod.invoke(obj);
              Object threadPolicyObject = buildMethod.invoke(obj);
              setThreadPolicyMethod.invoke(strictModeClass, threadPolicyObject);
  
          } catch (Exception ex) {
              String TAG = null;
              Log.w(TAG, ex);
          }
          super.onCreate(savedInstanceState);
          setContentView(R.layout.recomain);
          pd = (EditText) findViewById(R.id.name);
  
          error = (TextView) findViewById(R.id.showresult);
  
          pd.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View v) {
                  // TODO Auto-generated method stub
                  postParameters = new ArrayList&lt;NameValuePair&gt;();
                  postParameters.add(new BasicNameValuePair("product", pd
                          .getText().toString()));
  
  
                  // String valid = "1";
  
                  DownloadWebPageTask dw = new DownloadWebPageTask();
                  dw.execute("");
  
              }
          });
      }
  
      private class DownloadWebPageTask extends AsyncTask&lt;String, Void, String&gt; {
          @Override
          protected String doInBackground(String... urls) {
              String response = null;
              for (String url : urls) {
                  try {
                      response = CustomHttpClient.executeHttpPost("http://10.0.2.2/abc/check2.php", postParameters);  
                      String res = response.toString();
                      // res = res.trim();
                      res = res.replaceAll("\\s+", "");
                      // error.setText(res);
                      try{
                          res = "";
                    JSONArray jArray = new JSONArray(res);
                          for(int i=0;i&lt;jArray.length();i++){
                                  JSONObject json_data = jArray.getJSONObject(i);
                                  Log.i("prod_id","id: "+json_data.getInt("prod_id")+
                                          ", prod_name: "+json_data.getString("prod_name")+
                                          ", prod_category: "+json_data.getInt("prod_category")+
                                          ", prod_cost: "+json_data.getInt("prod_cost")
                                  );
                                  //Get an output to the screen
                                  res += "\n" + json_data.getString("prod_id") + " -&gt; "+ json_data.getInt("prod_name");
                          }
                  }
                  catch(JSONException e){
                          Log.e("log_tag", "Error parsing data "+e.toString());
                  }
  
                  try{
                   error.setText(res);
                  }
                  catch(Exception e){
                   Log.e("log_tag","Error in Display!" + e.toString());;          
                  }   
             }
                   catch (Exception e) {
              Log.e("log_tag","Error in http connection!!" + e.toString());     
             }
              }
              return response;
  
                 }
          @Override
          protected void onPostExecute(String result) {
          }
      }
  }
  </code></pre>
  
  <p>This is my php code:</p>
  
  <pre><code>&lt;?php
  $pd = $_POST['name'];
  
  //connect to the db
  $user = "root";
  $pswd = "";
  $db = "mylogin";
  $host = "localhost";
  $conn = mysql_connect($host, $user, $pswd);
  mysql_select_db($db);
  
  $query = "SELECT * FROM recoprod  ";
  $result = mysql_query("SELECT * FROM recoprod");
  while($row = mysql_fetch_array($result)) {
      $output[] = $row;
  }
  print(json_encode($output));
  mysql_close();
  /*
  $result = mysql_query($query) or die("Unable to verify user because : " . mysql_error());
  //this is where the actual verification happens  
  if(mysql_num_rows($result) &gt; 0)  
  echo 1;  // for correct login response  
  else  
  echo 0; // for incorrect login response */
  ?&gt;
  </code></pre>
  
  <p>Hi, I made some changes check the above edited code.</p>
  
  <p>And I have only 2 rows which I am fetching with each row containing 4 data.</p>