【问题标题】:Java Android Studio - exeception thrown on button click [duplicate]Java Android Studio - 单击按钮时引发的异常[重复]
【发布时间】:2021-06-01 01:01:15
【问题描述】:

我正在制作一个 Adnroid 应用,它使用 OkHTTP 在按钮点击时发送 API 请求

但是我在https://prnt.sc/10bbg1fhttps://prnt.sc/10bbgba这一行出现了这个错误

你能告诉我它有什么问题吗?

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;

import com.squareup.okhttp.Call;
import com.squareup.okhttp.Callback;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.Request;
import com.squareup.okhttp.Response;

import java.io.IOException;
public class MainActivity extends AppCompatActivity {
    OkHttpClient client = new OkHttpClient();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendMessage(View view) {
        //do something

        String url = "http://7d356d3464a8.ngrok.io/neo4j?query=Th%C3%A0nh%20ph%E1%BB%91%20H%E1%BB%93%20Ch%C3%AD%20Minh";
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        try {
            Response response = client.newCall(request).execute();
        }
        catch(IOException e) {
            e.printStackTrace();
        }
    }
}

【问题讨论】:

  • 用文本粘贴您的错误消息。您给定的链接没有任何错误消息

标签: java android android-studio okhttp


【解决方案1】:

您不应该在主线程上发出网络请求。 使用此方法

final Thread thread = new Thread(new Runnable() {
    @Override
    public void run() {
        try {
            Response response = client.newCall(request).execute();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
});

但是okhttp 让我们更容易使用enqueue method 发出异步请求:

client.newCall(request).enqueue(new Callback() {
    @Override
    public void onFailure(Call call, IOException e) {
        e.printStackTrace();
    }

    @Override
    public void onResponse(Call call, final Response response) throws IOException {
        if (!response.isSuccessful()) {
            throw new IOException("Unexpected code " + response);
        }

        // you code to handle response
    }
);

【讨论】:

    【解决方案2】:

    创建一个异步 GET 我们需要将调用排入队列。回调允许我们在响应可读时读取它。这发生在响应标头准备好之后。

    读取响应正文可能仍会阻塞。 OkHttp 目前不提供任何异步 API 来接收部分响应正文:

     public void sendMessage(View view) {
                String url = "http://7d356d3464a8.ngrok.io/neo4j?query=Th%C3%A0nh%20ph%E1%BB%91%20H%E1%BB%93%20Ch%C3%AD%20Minh";
                OkHttpClient client = new OkHttpClient();
                Request request = new Request.Builder()
                        .url(url)
                        .build();
    
                 Call call = client.newCall(request);
                 call.enqueue(new Callback() {
                public void onResponse(Call call, Response response) 
                  throws IOException {
                    // ...
                   }
                
                public void onFailure(Call call, IOException e) {
                    fail();
                }
            });
         }
    

    onResponse 的签名与你所拥有的不同。

    public void onResponse(Call call, Response response)

    public void onFailure(Call call, Throwable t)

    【讨论】:

    • 是的,我已经尝试过了,但出现了这个错误:prnt.sc/10bbrf3
    • onResponse 的签名和你的不一样。
    • public void onResponse(Call call, Response response) 和 public void onFailure(Call call, Throwable t)
    猜你喜欢
    • 1970-01-01
    • 2011-01-17
    • 1970-01-01
    • 1970-01-01
    • 2023-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多