2016-09-28 72 views
0

在第二個嘗試塊中發生錯誤,其中JSON數據是從php文件中獲取的,並且無法將其轉換爲JSONObjectAndroid:無法將字符串轉換爲以php格式寫入的json對象

以下是錯誤日誌

E/log_tag: <html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("e993d50cac6fa5e7cc6127986f6fb418");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://domainname.com/filename.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html> 

的Java文件

protected void onCreate(Bundle savedInstanceState) 
{ 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.detail); 
    Intent myIntent = getIntent(); 
    String Restaurant_Name = myIntent.getStringExtra("Restaurant_Name"); 
    System.out.println(Restaurant_Name); 

    InputStream is = null; 
    String result = null; 
    String v1 = Restaurant_Name; 
    ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(); 
    nameValuePairs.add(new BasicNameValuePair("f1",v1)); 
    try 
    { 
     HttpClient httpclient = new DefaultHttpClient(); 
     HttpPost httppost = new HttpPost("http_url/filename.php"); 
     httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs)); 
     HttpResponse response = httpclient.execute(httppost); 
     HttpEntity entity = response.getEntity(); 
     is = entity.getContent(); 
     Log.e("log_tag", "connection success for restDetail"); 
    } 
    catch(Exception e) 
    { 
     conn=true; 
     Log.e("log_tag", "Error in http connection for restDetail "+e.toString()); 
    } 
    if(conn==true) 
    { 
     Toast.makeText(getApplicationContext(), "Connection Fail for restDetail", Toast.LENGTH_SHORT).show(); 
     Toast.makeText(getApplicationContext(), "Something went wrong with the Internet for restDetail", Toast.LENGTH_SHORT).show(); 
    } 
    else 
    { 
    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"); 

     } 
     is.close(); 

     result=sb.toString(); 
     System.out.println(result); 
     Log.e("log_tag",""+result); 
    } 
    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"); 
      System.out.println(ch); 

      if(ch.equals("success")) { 
      //logic 
      } 
    } 
    catch(Exception e) 
    { 
     Toast.makeText(getApplicationContext(), "JSON parsing failed", Toast.LENGTH_SHORT).show(); 
    } 
    } 
} 

PHP代碼放在這裏..

<?php 
$con = mysqli_connect("online_hosting_server","username","password"); 
if (!$con) 
{ 
    die('Could not connect: ' . mysqli_error()); 
} 
else 
{ 
    echo"connected"; 
} 
mysqli_select_db("db_name", $con); 
$v1=$_REQUEST['f1']; 

$i=mysqli_query("select * from restaurant where Restaurant_Name='$v1'",$con); 
$check=''; 

while($row = mysqli_fetch_array($i)) 
{ 
    $r[]=$row; 
    $check=$row['Restaurant_Name']; 
} 

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

mysqli_close($con); 

?> 
+0

你的錯誤信息是錯誤的!請重新剪切並粘貼。 –

+0

使用Gson將字符串轉換爲json,這將非常容易。請參閱此鏈接https://sites.google.com/site/gson/gson-user-guide –

+0

代碼格式 – Nikhil

回答

-1

發佈PHP返回這裏。它應該是這樣的......

{"id_proposta":"1","id_pedido":"1","id_transportador":"2","id_cliente":"1","aceita":"1","finalizada":"1"}