我试图从我的java项目向服务器发送Json请求,问题是,我找到了一个人写的类,他使用Apache,我试图建立apache的路径,但它仍然不会导入这些东西:
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();
}
}