2015-10-18 50 views
0

嘿,我很喜歡用Java/Android中的HTTP請求。我想創建一個新的Github問題。所以我查了一下,幾乎每個人都以同樣的方式做了,但是我遇到了AndroidStudio告訴我的問題,所有的類(HttpClient, HttpPost, ResponseHandler)都不存在,是否有錯或爲什麼我沒有他們?HTTP請求正文中的POST JSON對象

private class GithubPostIssues extends AsyncTask<String, Void, String> { 

    private View view; 

    public GithubPostIssues(View view) { 
     this.view = view; 
    } 

    @Override 
    protected void onPostExecute(String result) { 
     Toast.makeText(FeedbackActivity.this, "Feedback/Hilfe gesendet", Toast.LENGTH_SHORT).show(); 
     finish(); 
    } 

    @Override 
    protected String doInBackground(String... params) { 
     return addIssue(view); 
    } 

    private String addIssue(View view) { 

     //here I'm getting some values from the user input and pass them in to the execute method 

     return execute(title, body, bug, help, question, feature, enhancement, design); 
    } 

    private String execute (String title, String body, boolean bug, 
          boolean help, boolean question, boolean feature, 
          boolean enhancement, boolean design) { 
     //Building the JSONOBject to pass in to the Request Body 
     JSONObject issue = new JSONObject(); 
     JSONArray labels = new JSONArray(); 
     try { 
      issue.put("title", title); 
      issue.put("body", body); 
      labels.put("0 - Backlog"); 
      if (bug) { 
       labels.put("bug"); 
      } 
      if (help) { 
       labels.put("help"); 
      } 
      if (question) { 
       labels.put("question"); 
      } 
      if (feature) { 
       labels.put("feature"); 
      } 
      if (enhancement) { 
       labels.put("enhancement"); 
      } 
      if (design) { 
       labels.put("design"); 
      } 
      issue.put("labels", labels); 

      return makeRequest("http://requestb.in/uwwzlwuw", issue); 
     } catch (JSONException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    private String makeRequest (String uri, JSONObject issue) { 
     //all these clases aren't found by Android Studio 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(uri); 

     httpPost.setEntity(new StringEntity(issue)); 
     httpPost.setHeader("Accept", "application/json"); 
     httpPost.setHeader("Content-type", "application/json"); 
     ResponseHandler<String> responseHandler = new BasicResponseHandler(); 
     return httpClient.execute(httpPost, responseHandler); 
    } 
} 

回答

0

嘗試增加這裏面gradle這個:

android { 
useLibrary 'org.apache.http.legacy' 
+0

雖然這解決了眼前的問題,我認爲這是不好的建議倡導使用賦予了更多的現代方法和框架傳統的HTTP方法。 –