2015-08-29 97 views
1

帮助!我如何在我的android工作室中启用以下httpclient?似乎无法找到NameValuePair,BasicNameValuePair,Httpclient,Httppost,显然我的HTTPConnectionParams是depracated?我如何解决它们?如何在android工作室中启用httpclient?

ArrayList<NameValuePair> dataToSend = new ArrayList<>(); 
      dataToSend.add(new BasicNameValuePair("name",user.name)); 
      dataToSend.add(new BasicNameValuePair("email",user.email)); 
      dataToSend.add(new BasicNameValuePair("password",user.password)); 

      HttpParams httpRequestParams = new BasicHttpParams(); 
      HttpConnectionParams.setConnectionTimeout(httpRequestParams, CONNECTION_TIMEOUT); 
      HttpConnectionParams.setSoTimeout(httpRequestParams, CONNECTION_TIMEOUT); 

      HttpClient client = new DefaultHttpClient(httpRequestParams); 
      HttpPost post = new HttpPost(SERVER_ADDRESS + "Register.php"); 

      try{ 
       post.setEntity(new UrlEncodedFormEntity(dataToSend)); 
       client.execute(post); 
      }catch (Exception e) { 
       e.printStackTrace(); 
      } 

回答

0

我想你可能使用SDK 23+,尝试使用的URLConnection或降级到SDK 22

0

我最近因为该库已被弃用,几乎所有的我的代码更改。我相信我们从现在起被建议使用原始的Java网络库。

请尝试以下

try{ 
    URL url = new URL(SERVER_ADDRESS + "Register.php"); 
    HttpURLConnection connection = (HttpURLConnection)url.openConnection(); 
    connection.setRequestMethod("POST"); 
    connection.setDoOutput(true); 
    connection.setConnectTimeout(CONNECTION_TIMEOUT); 
    String postData = URLEncoder.encode("name","UTF-8") 
         +"="+URLEncoder.encode(user.name,"UTF-8"); 
    postData += "&"+URLEncoder.encode("email","UTF-8") 
         +"="+URLEncoder.encode(user.email,"UTF-8"); 
    postData += "&"+URLEncoder.encode("password","UTF-8") 
         +"="+URLEncoder.encode(user.password,"UTF-8"); 
    OutputStreamWriter outputStreamWriter = new 
    OutputStreamWriter(connection.getOutputStream()); 
    outputStreamWriter.write(postData); 
    outputStreamWriter.flush(); 
    outputStreamWriter.close(); 
    }catch(IOException e){ 
    e.printStackTrace(); 
    } 

希望它可以帮助

0

BasicNameValuePair也已经过时。使用HashMap发送键和值。

HashMap的文档:http://developer.android.com/reference/java/util/HashMap.html

使用此方法,以数据公布至 “yourFiles.php”。

public String performPostCall(String requestURL, HashMap<String, String> postDataParams) { 

    URL url; 
    String response = ""; 
    try { 
     url = new URL(requestURL); 

     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setReadTimeout(15000); 
     conn.setConnectTimeout(15000); 
     conn.setRequestMethod("POST"); 
     conn.setDoInput(true); 
     conn.setDoOutput(true); 


     OutputStream os = conn.getOutputStream(); 
     BufferedWriter writer = new BufferedWriter(
       new OutputStreamWriter(os, "UTF-8")); 
     writer.write(getPostDataString(postDataParams)); 

     writer.flush(); 
     writer.close(); 
     os.close(); 
     int responseCode=conn.getResponseCode(); 

     if (responseCode == HttpsURLConnection.HTTP_OK) { 
      String line; 
      BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream())); 
      while ((line=br.readLine()) != null) { 
       response+=line; 
      } 
     } 
     else { 
      response=""; 

      throw new HttpException(responseCode+""); 
     } 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

    return response; 
} 
private String getPostDataString(Map<String, String> params) throws UnsupportedEncodingException { 
    StringBuilder result = new StringBuilder(); 
    boolean first = true; 
    for(Map.Entry<String, String> entry : params.entrySet()){ 
     if (first) 
      first = false; 
     else 
      result.append("&"); 

     result.append(URLEncoder.encode(entry.getKey(), "UTF-8")); 
     result.append("="); 
     result.append(URLEncoder.encode(entry.getValue(), "UTF-8")); 
    } 

    return result.toString(); 
} 
0

您也可以使用凌空图书馆谷歌让你完成工作。

RequestQueue queue = Volley.newRequestQueue(activity); 
       StringRequest strRequest = new StringRequest(Request.Method.POST, "Your URL", 
         new Response.Listener<String>() { 
          @Override 
          public void onResponse(String response) { 
           VolleyLog.d("Home_Fragment", "Error: " + response); 
           Toast.makeText(activity, "Success", Toast.LENGTH_SHORT).show(); 
          } 
         }, 
         new Response.ErrorListener() { 
          @Override 
          public void onErrorResponse(VolleyError error) { 
           VolleyLog.d(getApplicationContext(), "Error: " + error.getMessage()); 
           Toast.makeText(activity, error.toString(), Toast.LENGTH_SHORT).show(); 
          } 
         }) { 
        @Override 
        protected Map<String, String> getParams() { 
         Map<String, String> params = new HashMap<>(); 
         params.put("name", user.name); 
         params.put("email", user.email; 
         params.put("password", user.password); 

         return params; 
        } 
       }; 
       queue.add(strRequest); 

); 
:使用库的

实施例