0

I am pretty fresh with Android Studio, and I am trying to call MVC Api service with api key and app id.

However, whenever i try the one with api key and app id, it always throws "Trust anchor for certification path not found." while it works good when i try the one without api key.

Can you give me a advice? Thank you.

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button btn = (Button)findViewById(R.id.button);
    tv = (TextView)findViewById(R.id.textView);

    btn.setOnClickListener(new View.OnClickListener(){
        public void onClick(View v) {
            new JSONTask().execute("");

        }
    });
}

public class JSONTask extends AsyncTask<String, String, String>
{

    @Override
    protected String doInBackground(String... params) {
        try{
            StringBuilder result = new StringBuilder();
            URL url2 = new URL(url);
            HttpURLConnection conn = (HttpURLConnection) url2.openConnection();
            conn.setRequestMethod("GET");

            conn.setRequestProperty("Apikey",key);
            conn.addRequestProperty("AppID",id);

            BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = rd.readLine()) != null) {
                result.append(line);
            }
            rd.close();
            return result.toString();
        }

        catch (IOException e)
        {
            Log.e("MYAPP", "exception", e);
            return "error";
        }

    }
    protected void onPostExecute(String result)
    {
        super.onPostExecute(result);
        tv.setText(result);
    }
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

This kind of exceptions mostly occur because of the misconfiguration of a web service. Check out this article. It's fully described why you are getting this exception.

Abadii176
  • 113
  • 7