与json.JSONException的错误:值<br of =“”type =“”java.lang.string =“”can =“”be =“”converted =“”to =“”jsonobject =“”

时间:2017-03-21 08:31:59

标签: java php android mysql json

=“”

我正在尝试创建一个简单的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

1 个答案:

答案 0 :(得分:0)

记录你的JSON,你必须在html标签中获得resposnse。

尝试修复.php文件或尝试从JSON响应中filter html标记然后解析它