无法评估com.android.okhttp.HttpUrl $ Builder.toString()

时间:2018-01-21 11:37:32

标签: android nullpointerexception okhttp

以下代码似乎会导致抛出此异常:

public static String GET(String url) throws Exception {
    URL obj = new URL(url);
    HttpURLConnection con = (HttpURLConnection) obj.openConnection();
    con.setRequestMethod("GET");
    if (con.getResponseCode() == HttpURLConnection.HTTP_OK) {  <--**Here**
      ....
    }

..... }

这是MainActivity中调用此函数的方法:

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

    new AsyncTask<Void,Void,List<Client>>(){
        @Override
        protected void onPostExecute(List<Client> clients) {
            super.onPostExecute(clients);
            myList=clients;
        }

        @Override
        protected List<Client> doInBackground(Void... voids) {
            return getClients();

        }
    }.execute();

    if(myList == null)
    Log.v("Tag", "its null");
    else
        Log.v("Tag", "it works");
}

public List<Client> getClients() {
    List<Client> result = new ArrayList<Client>();
    try {
        String str = PHPtools.GET(WEB_URL + "Client.php");
        JSONArray array = new JSONObject(str).getJSONArray("Client");
        for (int i = 0; i < array.length(); i++) {
            JSONObject jsonObject = array.getJSONObject(i);
            Client client = new Client();
            client.setLastName(jsonObject.getString("lastName"));
            client.setFirstName(jsonObject.getString("firstName"));
            client.setId(jsonObject.getInt("_id"));
            client.setPhoneNumber(jsonObject.getString("phoneNumber"));
            client.setEmail(jsonObject.getString("email"));
            client.setCreditCardNumber(jsonObject.getLong("creditCardNumber"));

            result.add(client);
        }
        return result;

    } catch (Exception e) {
        e.printStackTrace();
    }
    return null;
}

}

我通过的网址已经过测试,效果很好。我尝试在Chrome和我试图解析的JSON显示完美。 但是当尝试在上述线路上建立连接时,问题就开始了。 我从AsyncTask调用此方法,并为Manifest文件添加了Internet权限。 这是我收到的完整错误: “方法抛出'java.lang.NullPointerException'异常。无法评估com.android.okhttp.HttpUrl $ Builder.toString()”

提前致谢

0 个答案:

没有答案