使用php,mysql在android中使用参数搜索和检索表值

时间:2015-03-12 05:01:19

标签: php android mysql

我开发了一个用于库的Android应用程序。我在搜索和检索基于书名或作者名称的书籍方面存在问题。表格已经创建但表格值未显示。并且在模拟器中显示&#34 ;记录不可用"。我在logcat中得到任何错误,我得到了连接成功"。我的php代码也正确地在localhost中检索数据..有人帮助我..

activity_screen6.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/index"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.library.Screen6" >

    <EditText
        android:id="@+id/detail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="19dp"
        android:ems="10"
        android:hint="Enter title/Author name"
        android:textColor="#ffffffff" />

    <Button
        android:id="@+id/searchBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/detail"
        android:layout_alignParentRight="true"
        android:text="Search"
        android:textColor="#ffffffff" />

    <TextView
               android:id="@+id/text1"
               android:layout_width="wrap_content"
               android:layout_height="wrap_content"

               android:layout_alignParentRight="true"
               android:layout_below="@+id/detail"
               android:layout_marginTop="54dp"
                android:textColor="#ffffffff" />



    <TableLayout
        android:id="@+id/table"
        android:layout_width="344dp"
        android:layout_height="fill_parent"
        android:focusable="true"
       android:layout_below="@+id/detail"
        android:focusableInTouchMode="true" >

</TableLayout>

</RelativeLayout>

Screen6.java

package com.example.library;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.os.Bundle;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TableRow.LayoutParams;

public class Screen6 extends Activity
{

    ProgressDialog dialog = null;
    Button button;
    //List<NameValuePair> nameValuePairs;
     public void onCreate(Bundle savedInstanceState)

     {
            super.onCreate(savedInstanceState);
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            setContentView(R.layout.activity_screen6);

             button = (Button) findViewById(R.id.searchBtn);
               final EditText f=(EditText)findViewById(R.id.detail);

            button.setOnClickListener(new View.OnClickListener()
            {
            public void onClick(View view)
              {
                //dialog = ProgressDialog.show(Screen6.this, "", 
                  //      "Searching...", true);
                 String result = null;
                InputStream is = null;

                try{
                        HttpClient httpclient = new DefaultHttpClient();
                        HttpPost httppost = new HttpPost("http://10.0.2.2:80/log/bksearch.php?&detail=detail");
                       // nameValuePairs = new ArrayList<NameValuePair>(1);
                        //nameValuePairs.add(new BasicNameValuePair("detail",f.getText().toString().trim())); 

                        //httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                        //Execute HTTP Post Request

                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();

                       // response=httpclient.execute(httppost);
                        is = entity.getContent();

                        Log.e("log_tag", "connection success ");

                        Toast.makeText(getApplicationContext(), "pass", Toast.LENGTH_SHORT).show();
                }
                catch(Exception e)
                {
                        Log.e("log_tag", "Error in http connection " +e.toString());
                        Toast.makeText(getApplicationContext(), "Connection fail", Toast.LENGTH_SHORT).show();

                }
                //convert response to string
                try
                {
                        BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
                        StringBuilder sb = new StringBuilder();
                        String line = null;
                        while ((line = reader.readLine()) != null)
                        {
                                sb.append(line + "\n");
                              //  Toast.makeText(getApplicationContext(), "Input Reading pass", Toast.LENGTH_SHORT).show();
                        }
                        is.close();

                        result=sb.toString();
                        Toast.makeText(getApplicationContext(), result, Toast.LENGTH_SHORT).show();

                }
                catch(Exception e)
                {
                       Log.e("log_tag", "Error converting result "+e.toString());
                    Toast.makeText(getApplicationContext(), " Input reading fail", Toast.LENGTH_SHORT).show();

                }

                //parse json data
                try
                {

                JSONArray jArray = new JSONArray(result);


                String re=jArray.getString(jArray.length()-1);


                TableLayout tv=(TableLayout) findViewById(R.id.table);
                tv.removeAllViewsInLayout();




                   int flag=1;

                for(int i=-1;i<jArray.length()-1;i++)

                        {




                                TableRow tr=new TableRow(Screen6.this);

                                tr.setLayoutParams(new LayoutParams(
                                           LayoutParams.FILL_PARENT,
                                           LayoutParams.WRAP_CONTENT));




                                if(flag==1)
                                {

                                    TextView b6=new TextView(Screen6.this);
                                     b6.setText("S.NO");
                                     b6.setTextColor(Color.WHITE);
                                     b6.setTextSize(15);
                                     tr.addView(b6);


                                   TextView b19=new TextView(Screen6.this);
                                     b19.setPadding(10, 0, 0, 0);
                                     b19.setTextSize(15);
                                     b19.setText("ACC NO");
                                     b19.setTextColor(Color.WHITE);
                                     tr.addView(b19);

                                   TextView b39=new TextView(Screen6.this);
                                     b39.setText("AUTHOR");
                                     b39.setTextColor(Color.WHITE);
                                     b39.setTextSize(15);
                                     tr.addView(b39);

                                   TextView b29=new TextView(Screen6.this);
                                     b29.setPadding(10, 0, 0, 0);
                                     b29.setText("TITLE");
                                     b29.setTextColor(Color.WHITE);
                                     b29.setTextSize(15);
                                     tr.addView(b29);

                                     TextView b59=new TextView(Screen6.this);
                                     b59.setText("EDITION");
                                     b59.setTextColor(Color.WHITE);
                                     b59.setTextSize(15);
                                     tr.addView(b59);

                                     TextView b79=new TextView(Screen6.this);
                                     b79.setText("YEAR");
                                     b79.setTextColor(Color.WHITE);
                                     b79.setTextSize(15);
                                     tr.addView(b79);

                                     TextView b49=new TextView(Screen6.this);
                                     b49.setText("PUBLISHER");
                                     b49.setTextColor(Color.WHITE);
                                     b49.setTextSize(15);
                                     tr.addView(b49);

                                     TextView b69=new TextView(Screen6.this);
                                     b69.setText("RS");
                                     b69.setTextColor(Color.WHITE);
                                     b69.setTextSize(15);
                                     tr.addView(b69);

                                     TextView b89=new TextView(Screen6.this);
                                     b89.setText("ISBN");
                                     b89.setTextColor(Color.WHITE);
                                     b89.setTextSize(15);
                                     tr.addView(b89);

                                     tv.addView(tr);

                                     final View vline = new View(Screen6.this);
                                          vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 8));
                                          vline.setBackgroundColor(Color.WHITE);



                                tv.addView(vline);
                                flag=0;


                                }

                                else
                                {



                                    JSONObject json_data = jArray.getJSONObject(i);


                                    Log.i("log_tag","S.NO: "+json_data.getInt("S.No")+
                                              ", ACC NO: "+json_data.getString("Acc No")+
                                              ", AUTHOR: "+json_data.getString("Author")
                                              +
                                              ", TITLE: "+json_data.getString("Title")
                                              +
                                              ", EDITION: "+json_data.getString("Edition")
                                              +
                                              ", YEAR: "+json_data.getString("Year")
                                              +
                                              ", PUBLISHER: "+json_data.getString("Publisher")
                                              +
                                              ", RS: "+json_data.getString("Rs")
                                              +
                                              ", ISBN: "+json_data.getString("ISBN"));




                            TextView b=new TextView(Screen6.this);
                                String stime=String.valueOf(json_data.getString("S.No"));
                                  b.setText(stime);
                                b.setTextColor(Color.RED);
                                b.setTextSize(15);
                                tr.addView(b);


                               TextView b1=new TextView(Screen6.this);
                                b1.setPadding(10, 0, 0, 0);
                                b1.setTextSize(15);
                                String stime1=json_data.getString("Acc No");
                                 b1.setText(stime1);
                                b1.setTextColor(Color.WHITE);
                                tr.addView(b1);

                              TextView b2=new TextView(Screen6.this);
                             b2.setPadding(10, 0, 0, 0);
                                String stime2=String.valueOf(json_data.getString("Author"));
                                b2.setText(stime2);
                                b2.setTextColor(Color.RED);
                                b2.setTextSize(15);
                                tr.addView(b2);

                                TextView b4=new TextView(Screen6.this);
                                b4.setPadding(10, 0, 0, 0);
                                b4.setTextSize(15);
                                String stime4=json_data.getString("Title");
                                 b4.setText(stime4);
                                b4.setTextColor(Color.WHITE);
                                tr.addView(b4);

                                TextView b5=new TextView(Screen6.this);
                                b5.setPadding(10, 0, 0, 0);
                                b5.setTextSize(15);
                                String stime5=json_data.getString("Edition");
                                 b5.setText(stime5);
                                b5.setTextColor(Color.WHITE);
                                tr.addView(b5);

                                TextView b6=new TextView(Screen6.this);
                                b6.setPadding(10, 0, 0, 0);
                                b6.setTextSize(15);
                                String stime6=json_data.getString("Year");
                                 b6.setText(stime6);
                                b6.setTextColor(Color.WHITE);
                                tr.addView(b6);

                                TextView b7=new TextView(Screen6.this);
                                b7.setPadding(10, 0, 0, 0);
                                b7.setTextSize(15);
                                String stime7=json_data.getString("Publisher");
                                 b7.setText(stime7);
                                b7.setTextColor(Color.WHITE);
                                tr.addView(b7);

                                TextView b8=new TextView(Screen6.this);
                                b8.setPadding(10, 0, 0, 0);
                                b8.setTextSize(15);
                                String stime8=json_data.getString("Rs");
                                 b8.setText(stime8);
                                b8.setTextColor(Color.WHITE);
                                tr.addView(b8);

                                TextView b9=new TextView(Screen6.this);
                                b9.setPadding(10, 0, 0, 0);
                                b9.setTextSize(15);
                                String stime9=json_data.getString("ISBN");
                                 b9.setText(stime9);
                                b9.setTextColor(Color.WHITE);
                                tr.addView(b9);

                                  tv.addView(tr);


                        final View vline1 = new View(Screen6.this);
                      vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 7));
                      vline1.setBackgroundColor(Color.WHITE);
                      tv.addView(vline1);      


                                }

                       }



                }
                catch(JSONException e)
                {
                        Log.e("log_tag", "Error parsing data "+e.toString());
                        Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
                }




           }
           });
     }



     }

bksearch.php

    <?php

         $con = mysql_connect("localhost","root","");
         if (!$con)
               {
             die('Could not connect: ' . mysql_error());
                }

           mysql_select_db("mydatabase", $con);

           $detail = $_GET['detail'];

           $i=mysql_query("select * from books where Title = '".$detail."' OR Author = '".$detail. "'",$con);

           $num_rows = mysql_num_rows($i);


           $check='';
          while($row = mysql_fetch_array($i))
            {

                  $r[]=$row;
                  $check=$row['S.No'];  
           }

         if($check==NULL)
           {           
                      $r[$num_rows]="Record is not available";
                      echo json_encode($r);

             }
            else
             {
                $r[$num_rows]="success";
                 echo json_encode($r);

              }

 mysql_close($con);

    ?>  

1 个答案:

答案 0 :(得分:0)

这里的sear数字为null。 因此,以编程方式发布f.getText().tostring()值。