0

我是 Android 开发的新手。

我正在尝试使用 JSON 在列表视图中基于用户 ID 获取 db 值。

我在stackoverflow上搜索了这个问题,但我找不到解决方案。

我不知道我得到了什么,JSONArray 或 JSONObject。

标题显示在 logcat 中。我已经尝试了下面提到的代码。

public class Search_Id extends Activity implements OnClickListener
{
    private static String url_search_by_id="php file in the remote server";

    private static final String REGISTER="regs";
    private static final String ID="id";
    private static final String FNAME="fname";
    private static final String LNAME="lname";
    private static final String PROFILEFOR="profilefor";
    private static final String AGE="age";

    private ProgressDialog PDialog;
    EditText Et_sear_id;    
    Button Btn_Search;

    TextView  search_id_detail_id,search_id_detail_fname,search_id_detail_lname,search_id_detail_profilefor,search_id_detail_age;

public void onCreate(Bundle b)
{
    super.onCreate(b);
    setContentView(R.layout.search_id);
    Et_sear_id=(EditText)findViewById(R.id.sear_et_id);
    Btn_Search=(Button)findViewById(R.id.sear_btn_search);
    Btn_Search.setOnClickListener(this);
}
@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.equals(Btn_Search))
    {
    //new SearchDetail().execute();
    final String st_id=Et_sear_id.getText().toString();

    if(st_id.length()>0)
    {
        ArrayList<NameValuePair> postparameter=new ArrayList<NameValuePair>();
        postparameter.add(new BasicNameValuePair("id",st_id));

        String response=null;
        try
        {
            response = CustomHttpClient.executeHttpPost("php file in the remote      server", postparameter);  
            String res=response.toString();
            //  res = res.trim();
            res= res.replaceAll("\\s+","");
            //error.setText(res);


            //Toast.makeText(getApplicationContext(), res, Toast.LENGTH_SHORT).show();
            if(res.equals("yes"))
            {   
                new SearchDetail().execute();
                Toast.makeText(getApplicationContext(), "Correct",Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Incorrect",Toast.LENGTH_SHORT).show();
                Et_sear_id.setText("");
            }
        }
        catch (Exception e) {
            // TODO: handle exception
            Toast.makeText(getApplicationContext(), e.toString(),Toast.LENGTH_LONG).show();
        }
    }
    else
    {
        Et_sear_id.setError("Provide ID to Search");
    }

    }
}

class SearchDetail extends AsyncTask<String,String,String>
{
    //final String St_sear_id1=Et_sear_id.getText().toString();

    @Override
    protected void onPreExecute()
    {
        super.onPreExecute();
        PDialog=new ProgressDialog(Search_Id.this);
        PDialog.setMessage("Loading...");
        PDialog.setIndeterminate(false);
        PDialog.setCancelable(true);
        PDialog.show();
    }
    @Override
    protected String doInBackground(String... arg0) {
        final String st_id=Et_sear_id.getText().toString();

        // TODO Auto-generated method stub
        List<NameValuePair> params=new ArrayList<NameValuePair>();          
        params.add(new BasicNameValuePair("id",st_id));

        JSONParser jsonParser=new JSONParser();
        JSONObject json=jsonParser.makeHttpRequest(url_search_by_id_guna,"POST",params);

        //Log.d("Create Response", json.toString());

        try
        {
            //Toast.makeText(getApplicationContext(), "Comes into Try Block", Toast.LENGTH_LONG).show();
            JSONArray jsonarray = json.getJSONArray(REGISTER);

            for(int i=0;i<jsonarray.length();i++)
            {
                JSONObject jo=jsonarray.getJSONObject(i);

                int id   = jo.getInt(ID);
                String fname = jo.getString(FNAME);
                String lname= jo.getString(LNAME);
                String profile_for=jo.getString(PROFILEFOR);
                String age=jo.getString(AGE);

                Intent in=new Intent(getApplicationContext(),Search_Id_Detail.class);
                //i.putExtra("REGISTER", regs);
                in.putExtra("ID",id);
                in.putExtra("FNAME",fname);
                in.putExtra("LNAME",lname);
                in.putExtra("PROFILEFOR",profile_for);
                in.putExtra("AGE",age);
                startActivity(in);          
            }

        }
        catch (Exception e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return null;
    }
    protected void onPostExecute(String arg)
    {
        PDialog.dismiss();
    }
}
}

PHP文件如下:

<?php

     /* Following code will list all products */

     //array for JSON response
     $response=array();

     $id=$_POST['id'];

     require_once 'vivagha_db_connect.php';
     //Connect to database
     $db=new DB_CONNECT();

     //get all products from products table
     $result=mysql_query("select *from regs where db_reg_id=$id") or die(mysql_error());

     //check for empty result
      if(mysql_num_rows($result)>0)
        {
          //looping through all results
          //products node
          //$response["regs"]=array();

          while($row=mysql_fetch_array($result))
          {
             //temp user array
             //$register=array();
             $response["id"]=$row["db_reg_id"];
             $response["fname"]=$row["db_reg_fname"];
             $response["lname"]=$row["db_reg_lname"];
             $response["profilefor"]=$row["db_reg_profilefor"];
             $response["age"]=$row["db_reg_age"];

             //push single product into final response array
             //array_push($response["regs"],$register);
             $response["success"]=1;
             $response["message"]="Found";
        }

        //success
        //response["success"]=1;

        //echo JSON response
        echo json_encode($response);
     }
     else
     {
        //no product found
        $response["success"]=0;
        $response["message"]="No Products Found";

         //echo no users JSON   
         //echo json_encode($response);
     }
    //} 
    ?>

我输入了用户的 id,然后如果 id 可用,则必须显示用户的详细信息。但它给了我标题所暗示的日志。

如果我能找到解决方案,这对我学习 android 会有帮助。


据我所知,问题在于您获取的是实际的 php 文件,而不是处理此类文件的结果。

这个问题实际上与android无关。尝试在您的网络浏览器上输入相同的地址,您将看到您为我们发布的相同 PHP 文件。

您需要配置一个服务器来处理该 PHP 文件并返回 JSON 响应。

4

1 回答 1

2

据我所知,问题在于您获取的是实际的 php 文件,而不是处理此类文件的结果。

这个问题实际上与android无关。尝试在您的网络浏览器上输入相同的地址,您将看到您为我们发布的相同 PHP 文件。

您需要配置一个服务器来处理该 PHP 文件并返回 JSON 响应。

于 2013-02-25T14:20:34.583 回答