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

使用php、mysql在android中使用参数搜索和检索表值,php,android,mysql,Php,Android,Mysql,我正在为图书馆开发一个android应用程序。我在根据书名或作者姓名搜索和检索图书时遇到问题。正在创建表,但未显示表值。在模拟器中,它显示“记录不可用”。我在logcat中也没有发现任何错误,我得到了“连接成功”。我的php代码也正确地检索本地主机中的数据。。有人帮帮我 activity_screen6.xml <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools

我正在为图书馆开发一个android应用程序。我在根据书名或作者姓名搜索和检索图书时遇到问题。正在创建表,但未显示表值。在模拟器中,它显示“记录不可用”。我在logcat中也没有发现任何错误,我得到了“连接成功”。我的php代码也正确地检索本地主机中的数据。。有人帮帮我

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();
                }




           }
           });
     }



     }
package com.example.library;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入java.util.List;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONArray;
导入org.json.JSONException;
导入org.json.JSONObject;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.graphics.Color;
导入android.os.Bundle;
导入android.os.StrictMode;
导入android.util.Log;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.TableLayout;
导入android.widget.TableRow;
导入android.widget.TextView;
导入android.widget.Toast;
导入android.widget.TableRow.LayoutParams;
公共类Screen6扩展活动
{
ProgressDialog=null;
按钮;
//列出nameValuePairs;
创建时的公共void(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
StrictMode.ThreadPolicy policy=新建StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(策略);
setContentView(R.layout.activity_屏幕6);
按钮=(按钮)findViewById(R.id.searchBtn);
最终EditText f=(EditText)findViewById(R.id.detail);
setOnClickListener(新视图.OnClickListener()
{
公共void onClick(视图)
{
//dialog=ProgressDialog.show(屏幕6.this,“,
//“搜索…”,没错);
字符串结果=null;
InputStream=null;
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://10.0.2.2:80/log/bksearch.php?&detail=detail");
//nameValuePairs=新的ArrayList(1);
//添加(新的BasicNameValuePair(“detail”,f.getText().toString().trim());
//setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行HTTP Post请求
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
//response=httpclient.execute(httppost);
is=entity.getContent();
Log.e(“Log_标签”,“连接成功”);
Toast.makeText(getApplicationContext(),“pass”,Toast.LENGTH\u SHORT.show();
}
捕获(例外e)
{
e(“Log_标记”,“http连接错误”+e.toString());
Toast.makeText(getApplicationContext(),“连接失败”,Toast.LENGTH\u SHORT.show();
}
//将响应转换为字符串
尝试
{
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is,“iso-8859-1”),8;
StringBuilder sb=新的StringBuilder();
字符串行=null;
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
//Toast.makeText(getApplicationContext(),“输入读取过程”,Toast.LENGTH\u SHORT.show();
}
is.close();
结果=sb.toString();
Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT.show();
}
捕获(例外e)
{
Log.e(“Log_标记”,“错误转换结果”+e.toString());
Toast.makeText(getApplicationContext(),“输入读取失败”,Toast.LENGTH\u SHORT.show();
}
//解析json数据
尝试
{
JSONArray jArray=新JSONArray(结果);
String re=jArray.getString(jArray.length()-1);
TableLayout tv=(TableLayout)findViewById(R.id.table);
tv.removeAllViewsInLayout();
int标志=1;
对于(inti=-1;i,这里的sear数为空。

所以以编程方式发布
f.getText().tostring()
value..

在数据库中添加书籍详细信息?
    <?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);

    ?>