2017-03-21 3 views
0

Ich versuche, eine einfache Android-App zu erstellen. Ich verwende wamp MySQL, um die Datenbank und den Server zu erstellen. Die Android-App soll Benutzerinformationen nach ID abrufen. Aber es gibt dieses Problem, als ich versuchte, es in Android-App zu machen. Dies ist das erste Mal, dass ich JSON verwende. Hilfe wird sehr geschätzt.Fehler mit json.JSONException: Wert <br des Typs java.lang.String kann nicht in JSONObject konvertiert werden

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-Datei

<?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-Code

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

    ?> 

Wann immer ich den Code ausführen, wird eine Fehlermeldung angezeigt werden:

03-21 00:02 : 37.560 6068-6068/com.example.user.database E/log_tag: Fehler pa rsing Daten org.json.JSONException: Wert br vom Typ java.lang.String nicht zu JSONObject

umgewandelt werden kann Ihre JSON

Antwort

0

anmelden, müssen Sie resposnse in HTML-Tag bekommen.

entweder versuchen, Sie zu filter HTML-Tags aus JSON-Antwort-Datei oder versuchen zu beheben .php es dann Danke analysieren

+0

für die Zeit, meine Frage zu beantworten. – Nizam

+0

bitte ich versuche, es zu bearbeiten, aber immer noch nicht – Nizam

+0

verwenden Log.d ("TAG", Leser); in Ihrem postMethod und zeigen Sie mir Protokolle hier – xbadal

Verwandte Themen