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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化