2017-10-16 114 views
0

平台:Android Studio中2.3.3
操作系统:Windows 10无法初始化类

我添加了所有httpcomponents-客户4.5.3 lib添加到我的项目的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总是跳转到罗oper.java。移动时,IDE无法解析Looper中的某个导入类。 enter image description here

我完全不知道这个问题,也许该SDK平台是不正确的。

+0

单独的Laeve'Looper',它的一切都很好。当我们的应用程序崩溃时,你会得到什么样的异常? –

+0

你不必处理looper,你只需要知道并使用它。祝你好运。 – KeLiuyue

回答

0

添加此dependency的build.gradle

dependencies { 
    compile 'com.intellij:annotations:[email protected]' 
    ... 
} 

或本

dependencies { 
    compile 'com.android.support:support-annotations:+' 
} 
0

你应该清理的项目,然后同步项目和改造项目。它工作正常。 但是,如果没有,你应该关闭android studio并重新打开它。

相关问题