-4

PHPサーバーリクエストを同等のJavaリクエストに変換することに非常にこだわっています。これは、JAVA でレプリケートして Android デバイスから送信する必要がある JSON オブジェクトを含むコードです。

$(".unableprocess").click(function() {
            if (!confirm("Confirm not able to process...!")) {
                return false;
            } else {
                var item_id = $(this).attr('data-id');
                var table_id = $(this).attr('table-id');
                var data = {
                    BookOrders: {
                        item_id: item_id,
                        table_id: table_id
                    }
                };
                $.ajax({
                    url:  //MY URL HERE ,
                    type: "POST",
                    data: data,
                    success: function(evt, responseText) {
                        location.reload();

                    }
                });
            }
});

そして、これが同じ機能を実行しようとする私の Java クラスです。このクラスは AsyncTask を拡張し、すべてのネットワーク対話は doInBackground() メソッドで発生します。これが私のコードです:

@Override
protected Boolean doInBackground(String... arg0) {

   try{


        HashMap<String, String> hashMap = new HashMap<String,String>();

        JSONObject jsonObject = new JSONObject();
        int statusCode;

        HttpClient client = new DefaultHttpClient();
        HttpPost httpPost = new HttpPost(tableMateCannotProcessURL);

        // JSON object creation begins here:

        jsonObject.accumulate("item_id",this.itemId);
        jsonObject.accumulate("table_id",this.tableId);

        JSONObject jObject = new JSONObject();

        jObject.accumulate("BookOrders", jsonObject);

        // JSON object ends here 

        Log.v("ATOMIC BLAST",jObject.toString());

        String json = jObject.toString();
        StringEntity se = new StringEntity(json);


        httpPost.setEntity(se);

        HttpResponse response = client.execute(httpPost);
        statusCode = response.getStatusLine().getStatusCode();
        Integer statusCodeInt = new Integer(statusCode);
        Log.v("HTTPResponse",statusCodeInt.toString());

        String result= "";
        StringBuilder builder = new StringBuilder();

        if (statusCode == 200) {
            HttpEntity entity = response.getEntity();
            InputStream content = entity.getContent();
            BufferedReader reader = new BufferedReader(new InputStreamReader(content));
            String line;
            while ((line = reader.readLine()) != null) { 
                builder.append(line); 
            }

            result = builder.toString();

        } 

        else { 
            Log.e("==>", "Failed to download file"); 
        }

    }
    catch(Exception e){
        e.printStackTrace();
    }

    return null;
}

作成した JSON オブジェクトは、コンソールに出力すると次のようになります。

{"BookOrders":{"table_id":"1","item_id":"2"}}

このオブジェクトをサーバーに POST した後、予期した応答が得られません。JSONオブジェクトをJAVAで同等のJSONオブジェクトに変換する適切な方法は何ですか? ガイダンス、指示、または解決策をいただければ幸いです。

4

2 に答える 2

0

php をバージョン 5.4 に更新すると役に立ちました。このバージョンjson_encode($x, JSON_PRETTY_PRINT)では、必要に応じて機能します。

于 2014-06-09T12:33:46.603 に答える
0

JSONは正しいようですが、オブジェクト内のオブジェクトです。

 JSONObject json = new JSONObject(yourdata);
 JSONObject jsonTable = new JSONObject(json.getString("BookOrders"));

 Log.d("JsonDebug", "json:" + jsonTable.toString());

JSONObject または Array があるかどうかわからない場合は、次を使用して検証できます。

String data = "{ ... }";
Object json = new JSONTokener(data).nextValue();
if (json instanceof JSONObject)
//you have an object
else if (json instanceof JSONArray)
//you have an array
于 2014-06-09T12:37:23.107 に答える