人格洞察错误

Personality Insight Error

FATAL EXCEPTION: main
Process: com.example.dell.ora, PID: 24491
android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1147)
at java.net.InetAddress.lookupHostByName(InetAddress.java:418)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:252)
at java.net.InetAddress.getAllByName(InetAddress.java:215)
at com.squareup.okhttp.Dns.lookup(Dns.java:39)
at com.squareup.okhttp.internal.http.RouteSelector.resetNextInetSocketAddress(RouteSelector.java:175)
at com.squareup.okhttp.internal.http.RouteSelector.nextProxy(RouteSelector.java:141)
at com.squareup.okhttp.internal.http.RouteSelector.next(RouteSelector.java:83)
at com.squareup.okhttp.internal.http.StreamAllocation.findConnection(StreamAllocation.java:174)
at com.squareup.okhttp.internal.http.StreamAllocation.findHealthyConnection(StreamAllocation.java:126)
at com.squareup.okhttp.internal.http.StreamAllocation.newStream(StreamAllocation.java:95)
at com.squareup.okhttp.internal.http.HttpEngine.connect(HttpEngine.java:281)
at com.squareup.okhttp.internal.http.HttpEngine.sendRequest(HttpEngine.java:224)
at com.squareup.okhttp.Call.getResponse(Call.java:286)
at com.squareup.okhttp.Call$ApplicationInterceptorChain.proceed(Call.java:243)
at com.squareup.okhttp.Call.getResponseWithInterceptorChain(Call.java:205)
at com.squareup.okhttp.Call.execute(Call.java:80)
at com.ibm.watson.developer_cloud.service.WatsonService.execute(WatsonService.java:122)
at com.ibm.watson.developer_cloud.service.WatsonService.executeRequest(WatsonService.java:183)
at com.ibm.watson.developer_cloud.personality_insights.v2.PersonalityInsights.getProfile(PersonalityInsights.java:119)
at com.example.dell.ora.AnalyzerFragment.onCreateView(AnalyzerFragment.java:39)

我正在尝试 运行 通过检查文档来了解我的性格

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View x = inflater.inflate(R.layout.fragment_analyzer, container, false);
        PersonalityInsights service = new PersonalityInsights();
        service.setUsernameAndPassword("", "");
        service.setEndPoint("https://gateway.watsonplatform.net/personality-insights/api");
        EditText content=  x.findViewById(R.id.content);
        TextView output=  x.findViewById(R.id.output);
//        String text = content.getText().toString();
        String text =getResources().getString(R.string.demo);
        Profile profile = service.getProfile(text);
        System.out.println(profile);
        return x;
    }

为此,我插入了我的 gradle 文件

compile 'com.squareup.okhttp3:okhttp:3.10.0'
    compile 'com.google.code.gson:gson:2.8.2'
    compile 'com.ibm.watson.developer_cloud:java-sdk:2.10.0'

有没有人遇到过这个错误?有人能证明我做错了什么吗?提前谢谢!

new Thread(new Runnable(){Profile profile = service.getProfile(text);
}).start();

或者使用异步任务。您只能在 onCreateView 中初始化布局和视图。所有其他对象初始化必须在您的 onActivityCreated

中完成
    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onActivityCreated(savedInstanceState);
      final  String text =getResources().getString(R.string.demo);

            new AsyncTask<Void, Void, Void>() {
                @Override
                protected Profile doInBackground(Void... params) {

        PersonalityInsights service = new PersonalityInsights();
                            service.setUsernameAndPassword("", "");
        service.setEndPoint("https://gateway.watsonplatform.net/personality-insights/api");

        Profile profile = service.getProfile(text);
       return profile;
               }

                @Override
                protected void onPostExecute(Profile profile) {
                    super.onPostExecute(profile);

        TextView output =  x.findViewById(R.id.output);
                 output.setText(profile.toString())
           }
              }.execute();

}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        final View x = inflater.inflate(R.layout.fragment_analyzer, container, false);
        EditText content=  x.findViewById(R.id.content);
        TextView output =  x.findViewById(R.id.output);
//        String text = content.getText().toString();
        return x;
    }