0

Please help me to find the error in below code.

I have posted a value from an Android file through namevaluepair to a PHP file.

I am getting the below error:

Notice: Undefined index: city {"result":[]}

<?php
$host = "localhost";
$user = "root";
$password = "";
$dbname = "sampledb";
$con = new mysqli($host, $user, $password, $dbname);
$city = $_POST['city'];
$con->query("SET NAMES 'utf8'");
$stmtsearch = "SELECT a.title, a.phone, a.description, a.email, a.post_date FROM `ad_master` a INNER JOIN tbl_city_master b ON a.city_id = b.fld_city_id WHERE b.fld_city_name = '$city'";
$resultsearch = $con->query($stmtsearch);
$json = array();
while ($row = mysqli_fetch_array($resultsearch)) {
    array_push($json, array(
        'title' => $row[0],
        'phone' => $row[1],
        'description' => $row[2],
        'email' => $row[3],
        'post_date' => $row[4]
    ));
}
echo json_encode(array("result" => $json));
$con->close();
?> 

Android code:

public void getData() {
    class GetDataJSON extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... params) {
            InputStream inputStream = null;
            String result = null;
            Intent in = getIntent();
            String city = in.getStringExtra(("city"));
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair("city", city));
            DefaultHttpClient httpClient = new DefaultHttpClient(new BasicHttpParams());
            HttpPost httppost = new HttpPost("http://10.0.2.2/qsearchdetail.php");
            try {
                HttpResponse response = httpClient.execute(httppost);
                HttpEntity entity = response.getEntity();
                inputStream = entity.getContent();
                BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "utf-8"), 8);
                StringBuilder sb = new StringBuilder();
                String line = null;
                while ((line = reader.readLine()) != null) {
                    sb.append(line + "\n");
                }
                result = sb.toString();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                try {
                    if (inputStream != null) inputStream.close();
                } catch (Exception squish) {
                }
                return result;
            }
        }

        @Override
        protected void onPostExecute(String result) {
            myJSON = result;
            showList();
        }
    }
    GetDataJSON g=new GetDataJSON();
    g.execute();
}

public void showList() {
    try {
        JSONObject jsonObj = new JSONObject(myJSON);
        search = jsonObj.getJSONArray(TAG_RESULT);
        for (int i = 0; i < search.length(); i++) {
            JSONObject c = search.getJSONObject(i);
            String title = c.getString(TAG_TITLE);
            String phone = c.getString(TAG_PHONE);
            String email = c.getString(TAG_EMAIL);
            String description = c.getString(TAG_DESCRIPTION);
            String postDate = c.getString(TAG_DATE);
            HashMap<String, String> search = new HashMap<String, String>();

            search.put(TAG_TITLE, title);
            search.put(TAG_PHONE, phone);
            search.put(TAG_EMAIL, email);
            search.put(TAG_DESCRIPTION, description);
            search.put(TAG_DATE, postDate);
            searchList.add(search);
        }
        ListAdapter adapter = new SimpleAdapter(
            QResultDetail.this,searchList, R.layout.search_result,
            new String[]{TAG_TITLE, TAG_PHONE, TAG_EMAIL, TAG_DESCRIPTION,TAG_DATE},
            new int[] { R.id.tvTitle, R.id.tvMobile, R.id.tvEmail, R.id.tvDesp, R.id.tvDate }
        );
        listView.setAdapter(adapter);
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
1
  • Your php code should start with checking if the city parameter comes in. Use isset() to check. If not set then send an error messge right away. Commented May 4, 2016 at 6:42

1 Answer 1

1

You are not sending your nameValuePairs.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.