0

電話からJson文字列を出力し、それをSQLサーバーにアップロードしようとしています。出力Jsonを取得してphpコードを記述する方法がわかりません...多くの方法を試しましたが、解決策が見つかりませんでした。

public void post(String string) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(
                "http://www.hopscriber.com/xoxoxox/testphp.php");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();

            nameValuePairs.add(new BasicNameValuePair("myJson", string));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);

            String str = inputStreamToString(response.getEntity().getContent())
                    .toString();
            Log.w("SENCIDE", str);
        } catch (Exception e) {
            Toast.makeText(getBaseContext(), "notwork", Toast.LENGTH_LONG)
                    .show();
        }
    }

    private Object inputStreamToString(InputStream is) {
        // TODO Auto-generated method stub
        String line = "";
        StringBuilder total = new StringBuilder();
        // Wrap a BufferedReader around the InputStream
        BufferedReader rd = new BufferedReader(new InputStreamReader(is));
        // Read response until the end
        try {
            while ((line = rd.readLine()) != null) {
                total.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
        // Return full string
        return total;
    }

json文字列を次のように出力します

[myJson=[{"name":"FriendTracker","user":"amjgp000000000000000","pack":"org.siislab.tutorial.friendtracker","perm":"org.siislab.tutorial.permission.READ_FRIENDS","level":"Normal"},{"name":"FriendTracker","user":"amjgp000000000000000","pack":"org.siislab.tutorial.friendtracker","perm":"org.siislab.tutorial.permission.WRITE_FRIENDS","level":"Normal"},{"name":"FriendTracker","user":"amjgp000000000000000","pack":"org.siislab.tutorial.friendtracker","perm":"org.siislab.tutorial.permission.FRIEND_SERVICE","level":"Normal"},{"name":"FriendTracker","user":"amjgp000000000000000","pack":"org.siislab.tutorial.friendtracker","perm":"org.siislab.tutorial.permission.FRIEND_NEAR","level":"Dangerous"},{"name":"FriendTracker","user":"amjgp000000000000000","pack":"org.siislab.tutorial.friendtracker","perm":"org.siislab.tutorial.permission.BROADCAST_FRIEND_NEAR","level":"Normal"},{"name":"FriendTracker","user":"amjgp000000000000000","pack":"org.siislab.tutorial.friendtracker","perm":"android.permission.RECEIVE_BOOT_COMPLETED","level":"Normal"},{"name":"FriendTracker","user":"amjgp000000000000000","pack":"org.siislab.tutorial.friendtracker","perm":"android.permission.READ_CONTACTS","level":"Dangerous"},{"name":"FriendTracker","user":"amjgp000000000000000","pack":"org.siislab.tutorial.friendtracker","perm":"android.permission.ACCESS_FINE_LOCATION","level":"Dangerous"},{"name":"FriendTracker","user":"amjgp000000000000000","pack":"org.siislab.tutorial.friendtracker","perm":"android.permission.WRITE_EXTERNAL_STORAGE","level":"Dangerous"},{"name":"FriendTracker","user":"amjgp000000000000000","pack":"org.siislab.tutorial.friendtracker","perm":"android.permission.READ_PHONE_STATE","level":"Dangerous"},{"name":"Tesing","user":"amjgp000000000000000","pack":"com.example.tesing","perm":"null","level":"null"},{"name":"Action Bar","user":"amjgp000000000000000","pack":"name.brucephillips.actionbarexample","perm":"null","level":"null"},.......

私が試した私のPHP

include("config.php");
$link = connect_db();

$jsonString=$_POST['myJson'];

$root = json_decode($jsonString, true);

foreach ($root as $item)
{
$user=$item['user'];
$name=$item['name'];
$pack=$item['pack'];
$perm=$item['perm'];
$level=$item['level'];

$sql ="INSERT INTO `Survey_Application_Data`(`User`, `Name`, `package`, `manifest_permission`, `protection_level`) VALUES ('$user','$name','$pack', '$perm','$level')";

$result = mysql_query($sql);

if ($result){

    echo "True";
}
else{
    echo "False";
}

}


?>
4

1 に答える 1

0

これを見てみましょうTo decode/phrase the JSON response from the PHP server . 次の方法で、応答を文字列に変換するだけです。

HttpEntity entity = response.getEntity();
String responseText = EntityUtils.toString(entity);

次に、それを JSON オブジェクトに変換し、それを使用getStringして新しいオブジェクトに割り当てます。String

JSONObject jsonResponseText = new JSONObject(responseText);
String name = jsonResponseText.getString("name");//To get the "name"
String user = jsonResponseText.getString("user");//To get the "user"
//etc...

EDITED 一度に1セットのデータのみをPHPサーバーに渡す場合は、各パラメーターをnameValuePairs

nameValuePairs.add(new BasicNameValuePair("name", "FriendTracker"));
nameValuePairs.add(new BasicNameValuePair("user", "amjgp000000000000000"));
//etc ...
//and execute the httppost

次に、phpコードで次を使用できます。

$name= $_POST["name"];
$user = $_POST["user"];
//etc ...
于 2012-12-10T05:02:01.383 に答える