在 Android 上使用 Loopj 实现基本 HttpClient Post

Implement Basic HttpClient Post with Loopj on Android

我是 Android 编程新手,在使用 Loopj 库实现消息发送器的基本版本时遇到了一些困难。

这是我的代码:

package com.test.app;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import com.loopj.android.http.*;

import org.apache.http.Header;

public class Test extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_test);

        AsyncHttpClient client = new AsyncHttpClient();
        client.get("https://www.google.com", new AsyncHttpResponseHandler() {

            @Override
            public void onStart() {
                // called before request is started
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[]     response) {
                // called when response HTTP status is "200 OK"
           }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {
                //called when response HTTP status is "4XX" (eg. 401, 403, 404)
            }

            @Override
            public void onRetry(int retryNo) {
                // called when request is retried
            }
        });

    }

}

这段代码给了我两个我正在尝试解决的错误:

  1. 行 AsyncHttpClient client = new AsyncHttpClient();下划线 AsyncHttpClient 并报告和错误:"Class must either be declared abstract or implement abstract method for onFailure"

  2. onSuccess 和 onFailure 的 @Override 指定带有下划线,错误状态为 "The method does not override the method from its superclass"。

如何在我的 Android Activity 中完成这项工作?

这是因为导入错误。

你应该替换:

import org.apache.http.Header;

import cz.msebera.android.httpclient.Header;

更新:

顺便说一句,如果你想制作 HTTP POST,你应该使用 .post() 而不是 .get()