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 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)