0

サーブレットと tomcat 6.0 を使用して Web サービスを作成しました。Web サービスを呼び出すためのサンプル Java アプリケーションを作成し、正常に動作しました。Web サービスの呼び出し中にデータを送信する必要があります。次のようにJavaアプリケーションで作成しました

StringEntity zStringEntityL = new StringEntity(zAPIInputStringP.toString());
zStringEntityL.setContentType(new BasicHeader(HTTP.CONTENT_TYPE,
        "application/json"));
HttpParams aHttpParamsL = new BasicHttpParams();
HttpProtocolParams.setVersion(aHttpParamsL, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(aHttpParamsL, HTTP.UTF_8);

SchemeRegistry aSchemeRegistryL = new SchemeRegistry();
aSchemeRegistryL.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));

ClientConnectionManager ccm = new ThreadSafeClientConnManager(aHttpParamsL, aSchemeRegistryL);
DefaultHttpClient client = new DefaultHttpClient(ccm, aHttpParamsL);
HttpPost aHttpPostL = new HttpPost(URL + zAPIName);
aHttpPostL.setHeader("Authorization", "Basic");



aHttpPostL.setEntity(zStringEntityL);
HttpResponse aHttpResponseL;
aHttpResponseL = client.execute(aHttpPostL); 

ここで、「zAPIInputStringP」は JSON 形式のデータです。
Webサービスでは、これらのデータをどのように取得する必要がありますか? eclispe でデバッグ モードでチェックしましたが、見つかりません。

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
{
    //How to get data?
}

私を助けてください。

4

2 に答える 2

2

post メソッドを介してサーブレットにデータを送信すると、入力ストリームを介してデータを利用できます。以下は、post メソッドがどのように見えるかです。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

String zAPIInputStringP = "";

BufferedReader in = new BufferedReader(new InputStreamReader(
                request.getInputStream()));
String line = in.readLine();
while (line != null) {
    zAPIInputStringP += line;
    line = in.readLine();
}


}

JSON 文字列は に含まれていzAPIInputStringPます。

于 2012-08-14T13:00:12.723 に答える
0

これははるかに簡単です。基本的には次のようになります。

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException 
    {
      response.setContentType("application/json");
    // Get the printwriter object from response to write the required json object to the output stream      
    PrintWriter out = response.getWriter();
    // Assuming your json object is **zStringEntityL**, perform the following, it will return your json object  
    StringEntity zStringEntityL = new StringEntity(zAPIInputStringP.toString());
    out.print(zStringEntityL);
    out.flush();
    }
于 2012-08-14T12:00:22.280 に答える