I am having problem integrating personality insights api in my android code. Although I followed a link from androidauthority and later changed the url and service credential values, nothing seem to work, error being THERE WAS AN ERROR. Also I know personality insights have explained
how to integrate the same but being an absolute beginner in java, I am having trouble integrating the same. Basically my aim is I have to accept input in plain text from the user and then later call the api and fetch back the results. The initial code is:
package com.shubham.myapplication;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.json.JSONTokener;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
EditText plainText;
TextView responseView;
ProgressBar progressBar;
static final String USER_NAME = "**USER_NAME**";
static final String PASS_WORD = "**PASS_WORD**";
static final String API_URL = "https://gateway.watsonplatform.net/personality-insights/api";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
responseView = (TextView) findViewById(R.id.responseView);
plainText = (EditText) findViewById(R.id.plainText);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
Button queryButton = (Button) findViewById(R.id.queryButton);
queryButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String text = plainText.getText().toString();
new RetrieveFeedTask().execute();
}
});
}
class RetrieveFeedTask extends AsyncTask<Void, Void, String> {
private Exception exception;
protected void onPreExecute() {
progressBar.setVisibility(View.VISIBLE);
responseView.setText("");
}
protected String doInBackground(Void... urls) {
String text = plainText.getText().toString();
// Do some validation here
try {
URL url = new URL(API_URL + "text=" + text + "&username=" + USER_NAME + "&password=" + PASS_WORD);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}
bufferedReader.close();
return stringBuilder.toString();
}
finally{
urlConnection.disconnect();
}
}
catch(Exception e) {
Log.e("ERROR", e.getMessage(), e);
return null;
}
}
protected void onPostExecute(String response) {
if(response == null) {
response = "THERE WAS AN ERROR";
}
progressBar.setVisibility(View.GONE);
Log.i("INFO", response);
responseView.setText(response);
// TODO: check this.exception
// TODO: do something with the feed
// try {
// JSONObject object = (JSONObject) new
JSONTokener(response).nextValue();
// String requestID = object.getString("requestId");
// int likelihood = object.getInt("likelihood");
// JSONArray photos = object.getJSONArray("photos");
// .
// .
// .
// .
// } catch (JSONException e) {
// e.printStackTrace();
// }
}
}
}
Please tell me what to do to successfully fetch the results!