2016-06-01 4 views
0

私はトークン、ブロックリクエスト、およびフォーマットとしてリクエストパラメータを持つURLをhttps://www.xyz.in/ws/btとしています。 サンプルJSON「blockrequest」文字列はアンドロイドでHttpsURLConnectionを使用してURL内のリクエストパラメータとしてjsonオブジェクトを送信する方法

私は のHttpsURLConnectionを使用して、URLに リクエストパラメータとして、このデータを送信することができますどのように
{"source\":\"1492\",\"destination\":\"1406\",\"availableTripId\":\"100008417320611112\",\"boardingPointId\":\"1129224\",\"inventoryItems\":[{\"seatName\":\"21\",\"ladiesSeat\":\"false\",\"passenger\":{\"name\":\"passenger_name_1\",\"title\":\"MR\",\"gender\":\"MALE\",\"age\":\"23\",\"primary\":true,\"idType\":\"PANCARD\",\"email\":\"[email protected]_name.com\",\"idNumber\":\"BEPS1111B\",\"address\":\"passenger_address\",\"mobile\":\"xxxxxxxxxx\"},\"fare\":\"320.00\"},{\"seatName\":\"22\",\"ladiesSeat\":\"true\",\"passenger\":{\"name\":\"passenger_name_1\",\"title\":\"MS\",\"gender\":\"FEMALE\",\"age\":\"23\",\"primary\":false,\"idType\":\"\",\"email\":\"\",\"idNumber\":\"\",\"address\":\"\",\"mobile\":\"\"},\"fare\":\"320.00\"}]} 

です。

+0

(応答は、接続のgetInputStream()メソッドを使用して読み出すために)「リクエストパラメータとして」あなたは、HTTP GETリクエストとして意味ですか? – Robert

+0

いいえ、HTTPS POST要求を使用します。 – QEMU

+1

http://stackoverflow.com/a/2938787/793943これをチェックしてください – Sush

答えて

0

Apache HTTPクライアントを使用している場合。ここでのサンプルコードは、

protected void send(final String json) { 
     Thread t = new Thread() { 

      public void run() { 
       Looper.prepare(); //For Preparing Message Pool for the child Thread 
       HttpClient client = new DefaultHttpClient(); 
       HttpConnectionParams.setConnectionTimeout(client.getParams(), 10000); //Timeout Limit 
       HttpResponse response; 

       try { 
        HttpPost post = new HttpPost(URL); 
        StringEntity se = new StringEntity(json.toString()); 
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
        post.setEntity(se); 
        response = client.execute(post); 

        /*Checking response */ 
        if(response!=null){ 
         InputStream in = response.getEntity().getContent(); //Get the data in the entity 
        } 

       } catch(Exception e) { 
        e.printStackTrace(); 
        createDialog("Error", "Cannot Estabilish Connection"); 
       } 

       Looper.loop(); //Loop in the message queue 
      } 
     }; 

     t.start();  
    } 

だこれは、上記のサンプルコードでのIMPラインです:

StringEntity se = new StringEntity(json.toString()); 
        se.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, "application/json")); 
        post.setEntity(se); 

はそれを動作this.I希望をお試しください。あなたはこのような何か行うことができます

+0

API 23(Android 6)でApache HTTPクライアントが廃止されました。 – Robert

+0

代わりにVolleyライブラリを使用 –

+0

はい。ボレーを使用するか改造する。要求に応じてjson本体を簡単に送信することができます。 –

0

URL url = new URL(yourUrl); 
byte[] postData = yourJsonString.getBytes("UTF-8"); 
HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 

conn.setRequestMethod("POST"); 
conn.setRequestProperty("Content-Type", "application/json"); 
conn.setDoOutput(true); 

conn.getOutputStream().write(postDataBytes); 

関連する問題