douluan5738 2013-02-25 14:16
浏览 219
已采纳

解析数据时出错org.json.JSONException:值<!DOCTYPE类型java.lang.String无法转换为JSONObject

I am new to Android Development.

I am trying to fetch the db values based on userid in a list view using JSON.

I searched stackoverflow for this problem, But I can't able to find a soln for this.

I don't know what I am getting, JSONArray or JSONObject.

The title was shown in the logcat. I have tried the below mentioned code.

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 File is following:

<?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);
     }
    //} 
    ?>

I entered the user's id, then If the id is available in the user's details must be shown. But It gives me the log as the title suggests.

It would be helpful for me to learn android, If I can get a solution for this.

  • 写回答

1条回答 默认 最新

  • doufei2662 2013-02-25 14:20
    关注

    as far as I can see the problem is that you're getting the actual php file and not the result from the processing of such file.

    The problem actually have NOTHING to do with android. Try typing the same address on your web-browser and you'll see this same PHP file you posted for us.

    You need to configure a server to process that PHP file and return the JSON response.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch