Search code examples
androidibm-cloudibm-watsonpersonality-insights

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$1.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)

I am trying to run personality insight from checking the docs I have this in my fragment

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

And to do this I inserted in my gradle file

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'

Has anybody encountered this error?Can sb make evident what am I making wrong?Thank You in advance!


Solution

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

    Or use an async task. You must only initialize layouts and Views in your onCreateView. All other object initializations must be done in your 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;
        }