简体   繁体   English

正文中的HTTP请求POST JSON对象

[英]HTTP Request POST JSON Object in Body

Hey guys I'm struggeling with HTTP Requests in Java/Android. 大家好,我在Java / Android中忙于HTTP请求。 I want to create a new Github issue. 我想创建一个新的Github问题。 So I've looked it up, and almost everybody did it the same way, but I have the problem that AndroidStudio tells me, that all the classes ( HttpClient, HttpPost, ResponseHandler ) doesn't exist, did I something wrong or why I don't have them? 因此,我进行了查找,几乎每个人都以相同的方式进行了操作,但是我遇到了一个问题,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);
    }
}

Try adding this inside gradle: 尝试在gradle中添加以下内容:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM