2017-04-06 18 views
-2

私のJavaプロジェクトからサーバーにJsonリクエストを送信しようとしていますが、問題は、誰かが書いたクラスが見つかりました。 Apacheは、私はこのようなものをApacheのパスを作ってみましたが、それはまだdoesntのインポート:Jsonのリクエストをサーバーjavaプロジェクト(NetBeans)に送信するには

import org.apache.http.HttpResponse; 
    import org.apache.http.client.methods.HttpPost; 
    import org.apache.http.entity.StringEntity; 
    import org.apache.http.impl.client.DefaultHttpClient; 

はここにクラスです:

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStreamReader; 
import java.io.UnsupportedEncodingException; 
import java.net.MalformedURLException; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.methods.HttpPost; 
import org.apache.http.entity.StringEntity; 
import org.apache.http.impl.client.DefaultHttpClient; 


public class JsonHTTPRequest { 
    private static String postToURL(String url, String message, DefaultHttpClient httpClient) throws IOException, IllegalStateException, UnsupportedEncodingException, RuntimeException { 
     HttpPost postRequest = new HttpPost(url); 

     StringEntity input = new StringEntity(message); 
     input.setContentType("application/json"); 
     postRequest.setEntity(input); 

     HttpResponse response = httpClient.execute(postRequest); 

     if (response.getStatusLine().getStatusCode() != 200) { 
      throw new RuntimeException("Failed : HTTP error code : " 
        + response.getStatusLine().getStatusCode()); 
     } 

     BufferedReader br = new BufferedReader(
       new InputStreamReader((response.getEntity().getContent()))); 

     String output; 
     StringBuffer totalOutput = new StringBuffer(); 
     System.out.println("Output from Server .... \n"); 
     while ((output = br.readLine()) != null) { 
      System.out.println(output); 
      totalOutput.append(output); 
     } 
     return totalOutput.toString(); 
    } 
} 

答えて

0

apatcheは、第三者であるあなたがapatche libが含まれるように持っているので、見てみましょうこの時page

関連する問題