Android:无法将字符串转换为以php格式编写的json对象

时间:2016-09-28 17:59:06

标签: php android json

在第二个尝试块中发生错误,其中JSON数据从php文件中提取,并且无法将其转换为JSONObject

以下是错误日志

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

?> 

1 个答案:

答案 0 :(得分:-1)

在这里发布PHP的回复。它应该像... ...

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