平台:Android Studio 2.3.3
操作系统:Windows 10
我已将所有httpcomponents-client-4.5.3 lib添加到我的项目库中,并创建新类HttpUtil:
package com.zsh.ricky.zsh.util;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
/**
* Created by Ricky on 2017/9/29.
*/
public class HttpUtil {
//创建HttpClient对象
private static CloseableHttpClient httpClient = HttpClients.createDefault();
private static final String BASE_URL = "http://10.0.2.2:8080/CGService/";
/**
* 向服务器发送get请求
* @param url 发送请求的url
* @return 服务器响应字段
* @throws ExecutionException
* @throws InterruptedException
*/
public static String getRequest(final String url)
throws ExecutionException, InterruptedException
{
FutureTask<String> task = new FutureTask<String>(
new Callable<String>() {
@Override
public String call() throws Exception {
//创建 HttpGet对象
HttpGet get = new HttpGet(BASE_URL + url);
//发送get请求
CloseableHttpResponse response = httpClient.execute(get);
String result = null;
try {
HttpEntity entity = response.getEntity();
if (entity != null)
{
//获取服务器响应字符串
result = EntityUtils.toString(entity);
}
} finally {
response.close();
}
return result;
}
}
);
new Thread(task).start();
return task.get();
}
/**
* 处理POST请求
* @param url 发送请求的url
* @param rawParams 请求参数
* @return 服务器响应字符串
* @throws ExecutionException
* @throws InterruptedException
*/
public static String postRequest(final String url,
final Map<String, String> rawParams)
throws ExecutionException, InterruptedException
{
FutureTask<String> task = new FutureTask<String>(
new Callable<String>() {
@Override
public String call() throws Exception {
//创建httpPost对象
HttpPost post = new HttpPost(BASE_URL + url);
//如果传递的参数比较多,对参数进行封装
List<NameValuePair> params = new ArrayList<>();
for (String key : rawParams.keySet())
{
//封装请求参数
params.add(new BasicNameValuePair(key, rawParams.get(key)));
}
//设置请求参数
post.setEntity(new UrlEncodedFormEntity(params, "utf-8"));
//发送post请求
CloseableHttpResponse response = httpClient.execute(post);
String result = null;
try {
HttpEntity entity = response.getEntity();
if (entity != null)
{
//获取响应字符串
result = EntityUtils.toString(entity);
}
} finally {
response.close();
}
return result;
}
}
);
new Thread(task).start();
return task.get();
}
public static String get(final String url)
throws IOException
{
//创建 HttpGet对象
HttpGet get = new HttpGet(BASE_URL + url);
//发送get请求
CloseableHttpResponse response = httpClient.execute(get);
String result = null;
try {
HttpEntity entity = response.getEntity();
if (entity != null)
{
//获取服务器响应字符串
result = EntityUtils.toString(entity);
}
} finally {
response.close();
}
return result;
}
}
当我想在MainActivity中初始化类时:
package com.zsh.ricky.zsh;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.zsh.ricky.zsh.util.DialogUtil;
import com.zsh.ricky.zsh.util.HttpUtil;
public class MainActivity extends AppCompatActivity {
private TextView mainText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String url = "login";
String result = "";
try {
result = HttpUtil.getRequest(url);
} catch (Exception e) {
DialogUtil.showDialog(this, "服务器响应出错!", false);
e.printStackTrace();
}
mainText = (TextView) this.findViewById(R.id.main_text);
mainText.setText(result);
}
}
应用程序将停止,当我调试此代码时,IDE总是跳转到Looper.java。转移,IDE无法解析Looper中的某些导入类。
我完全不知道这个问题,也许sdk平台不对。
答案 0 :(得分:0)
将此dependency
添加到 build.gradle
dependencies {
compile 'com.intellij:annotations:+@jar'
...
}
或者
dependencies {
compile 'com.android.support:support-annotations:+'
}
答案 1 :(得分:0)
您应该清理项目,然后同步项目并重建项目。它工作正常。 但如果没有,你应该关闭android工作室并重新打开它。