我正在开发防盗应用程序,这是我的连接http
类。
我有错误,我无法解决((无法解析setConfig,setAttribute和setHeader)) 我该怎么做才能解决它?
以下是代码:
package com.example.fatooma.location;
/**
* Created by fatooma on 26/07/2017.
*/
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import com.example.fatooma.location.Core;
public class HttpRmi {
static String url = "http://localhost/projectserver";
CloseableHttpClient httpClient = HttpClients.createDefault();
// Request configuration can be overridden at the request level.
// They will take precedence over the one set at the client level.
RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).build();
HttpGet httpget = new HttpGet(url);
httpget.setConfig(requestConfig);
httpget.setHeader("User-Agent","Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.2.13) Gecko/20101206 Firefox/3.6.13");
// Execution context can be customized locally.
HttpClientContext context = HttpClientContext.create();
// Contextual attributes set the local context level will take
// precedence over those set at the client level.
context.setAttribute("http.protocol.version",HttpVersion.HTTP_1_1);
try {
// Execute the method
HttpResponse response = httpClient.execute(httpget);
int statusCode = response.getStatusLine().getStatusCode();
if (statusCode != HttpStatus.SC_OK) {
throw new IllegalStateException("Method failed: " + response.getStatusLine());
}
BufferedReader br = new BufferedReader(new InputStreamReader((response.getEntity().getContent())));
StringBuffer buf = new StringBuffer();
String output;
while ((output = br.readLine()) != null) {
buf.append(output);
}
content = buf.toString();
} catch(Exception e ) {
throw e;
}
} finally
httpClient.close()
}}