【问题标题】:HTTP Request POST JSON Object in Body正文中的 HTTP 请求 POST JSON 对象
【发布时间】:2015-10-18 21:39:51
【问题描述】:

大家好,我正在为 Java/Android 中的 HTTP 请求而苦苦挣扎。我想创建一个新的 Github 问题。所以我查了一下,几乎每个人都这样做了,但我遇到了 AndroidStudio 告诉我的问题,所有类 (HttpClient, HttpPost, ResponseHandler) 都不存在,是我做错了什么还是为什么我不存在?没有吗?

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);
    }
}

【问题讨论】:

    标签: android json post httprequest github-api


    【解决方案1】:

    尝试在 gradle 中添加这个:

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

    【讨论】:

    • 虽然这解决了眼前的问题,但我认为在更现代的方法和框架下提倡使用旧的 HTTP 方法是一个糟糕的建议。
    猜你喜欢
    • 1970-01-01
    • 2015-03-19
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多