【问题标题】:ERROR with json.JSONException: Value <br of type java.lang.String cannot be converted to JSONObjectjson.JSONException 错误:java.lang.String 类型的值 <br 无法转换为 JSONObject
【发布时间】:2017-08-12 19:24:03
【问题描述】:

我正在尝试创建一个简单的 Android 应用程序。我正在使用 wamp MySQL 创建数据库和服务器。 android 应用程序应该通过 ID 获取用户信息。但是当我尝试将其放入Android应用程序时出现了这个问题。这是我第一次使用 JSON。 非常感谢您的帮助。

 public class SelectActivity extends Activity {

 StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();


public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_select);

    Button button = (Button) findViewById(R.id.button1);
    StrictMode.setThreadPolicy(policy);

    button.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View view)
        {
            String result = null;
            InputStream is = null;
            EditText editText = (EditText)findViewById(R.id.e1);
            String v1 = editText.getText().toString();
            EditText editText1 = (EditText)findViewById(R.id.e2);

            EditText editText2 = (EditText)findViewById(R.id.e3);

            ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("f1",v1));
            try
            {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://192.168.0.16/view/select.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost);
                HttpEntity entity = response.getEntity();
                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();
            }
            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{


                JSONObject object = new JSONObject(result);
                String ch=object.getString("re");
                if(ch.equals("success"))
                {

                    JSONObject no = object.getJSONObject("0");

                    //long q=object.getLong("f1");
                    String w= no.getString("f2");
                    long e=no.getLong("f3");

                    editText1.setText(w);
                    String myString = NumberFormat.getInstance().format(e);


                    editText1.setText(myString);

                      }
                        else
                      {

                    Toast.makeText(getApplicationContext(), "Record is not available.. Enter valid number", Toast.LENGTH_SHORT).show();

                     }

                  }
            catch(JSONException e)
            {
                Log.e("log_tag", "Error parsing data "+e.toString());
                Toast.makeText(getApplicationContext(), "JsonArray fail", Toast.LENGTH_SHORT).show();
            }


                    }
                   });



                }


           }

XML 文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<EditText
    android:id="@+id/e1"
    android:numeric="integer"
    android:hint=" ID"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <requestFocus />
</EditText>


<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="SELECT" />


<EditText
    android:id="@+id/e2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="name"/>

<EditText
    android:id="@+id/e3"
    android:hint="number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />


 </LinearLayout>  

PHP 代码

  <?php

     $con = mysql_connect("localhost","root","");
     if (!$con)
       {
         die('Could not connect: ' . mysql_error());
       }

       mysqli_select_db("ex1", $con);
       $v1=$_REQUEST['f1'];
      if($v1==NULL)
        {


            $r["re"]="Enter the number!!!";
             print(json_encode($r));
            die('Could not connect: ' . mysql_error());
      }

      else

        {


            $i=mysqli_query("select * from t1 where f1=$v1",$con);
           $check='';
           while($row = mysql_fetch_array($i))
            {

              $r[]=$row;
              $check=$row['f1'];
             }
              if($check==NULL)
               {            
                  $r["re"]="Record is not available";
                  print(json_encode($r));

                 }
               else
                 {
                     $r["re"]="success";
                        print(json_encode($r));

                   } 



            }

     mysql_close($con);

     ?>

当我运行代码时,它会显示一条错误消息:

03-21 00:02:37.560 6068-6068/com.example.user.database E/log_tag: 解析数据时出错 org.json.JSONException:值 br 类型 java.lang.String 无法转换为 JSONObject

【问题讨论】:

    标签: java php android mysql json


    【解决方案1】:

    记录您的 JSON,您必须在 html 标记中得到响应。

    要么尝试修复你的.php文件,要么尝试从JSON响应中filter html标签然后解析它

    【讨论】:

    • 感谢您抽出时间回答我的问题。
    • 使用 Log.d("TAG",reader);在您的 postMethod 中并在此处显示日志
    • 我对 android studio 真的很陌生。坦率地说,我不明白你要我做什么。请详细说明
    • Log.e("log_tag", "result: "+result); JSONObject 对象 = 新 JSONObject(结果);字符串 ch=object.getString("结果"); if(ch.equals("success"))
    猜你喜欢
    • 1970-01-01
    • 2020-04-02
    • 2013-08-02
    • 2023-03-10
    • 1970-01-01
    • 2021-10-23
    • 1970-01-01
    • 2015-09-19
    相关资源
    最近更新 更多