当前位置: 首页 > 知识库问答 >
问题:

获取错误java.lang.IllegalArgumentException:意外的url

江佐
2023-03-14
public class LoginActivity extends AppCompatActivity { **// Showing Error at this LIne**

public Location location;
private Button btnLogin;
private boolean doubleBackToExitPressedOnce = false;
private EditText phoneNo, password;
private CheckBox cbShow, cbRemember;
private NetworkUtil networkUtil;
private SharePrefUtil sharePref;
private LocationInfo locationInfo;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    networkUtil = new NetworkUtil(getApplicationContext());
    sharePref = new SharePrefUtil(getApplicationContext());
    initScreen();

    if (sharePref.getValueFromSharePref("remeberFlag").equalsIgnoreCase("true")) {
        phoneNo.setText(sharePref.getValueFromSharePref("mobileno"));
        password.setText(sharePref.getValueFromSharePref("password"));
        cbRemember.setChecked(true);
    }
}



private void initScreen() {
    LocationLibrary.showDebugOutput(true);
    try {
        LocationLibrary.initialiseLibrary(LoginActivity.this, 60 * 1000, 60 * 1000 * 2, "com.aspeage.jagteraho");

    } catch (UnsupportedOperationException e) {
        Toast.makeText(this, "Device doesn't have any location providers", Toast.LENGTH_LONG).show();
    }

    phoneNo = (EditText) findViewById(R.id.ed_phoneno);
    password = (EditText) findViewById(R.id.ed_password);
    cbRemember = (CheckBox) findViewById(R.id.cbox_rememberme);
    cbRemember.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) sharePref.setValueInSharePref("remeberFlag", "true");
            else sharePref.setValueInSharePref("remeberFlag", "false");
        }
    });

    cbShow = (CheckBox) findViewById(R.id.cbox_showpass);
    cbShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                password.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            } else {
                password.setInputType(129);
            }
        }
    });
    btnLogin = (Button) findViewById(R.id.btn_login);
    btnLogin.setOnClickListener(new ButtonClick());
}

private class ButtonClick implements View.OnClickListener {

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_login:
                btnLoginClicked();
                break;
            default:
                break;
        }
    }
}

private void btnLoginClicked() {
    if(phoneNo.getText().toString().trim().equals("admin") && password.getText().toString().trim().equals("admin")) {
        loginService();
    }
    if (validation()) {
        if (cbRemember.isChecked())
            rememberMe(password.getText().toString().trim());
        if (networkUtil.isConnected()) {
            loginService();
        } else {
            new SweetAlertDialog(LoginActivity.this, cn.pedant.SweetAlert.SweetAlertDialog.ERROR_TYPE)
                    .setTitleText("Oops...")
                    .setContentText("No Network Connection")
                    .show();
        }
    }
}

/**
 * save username and password in SharedPreferences.
 *
 * @param //password is key value for storing in SharedPreferences.
 */
public void rememberMe(String password) {
    SharePrefUtil sharePref = new SharePrefUtil(getApplicationContext());
    sharePref.setValueInSharePref("password", password);
}

private boolean validation() {
    int errorCount = 0;
    if (phoneNo.getText().toString().trim().equals("")
            || phoneNo.getText().length() != 10) {
        phoneNo.setError("Enter valid phone number");
        errorCount = errorCount + 1;
        if (errorCount == 1) {
            phoneNo.requestFocus();
        }
    } else {
        phoneNo.setError(null);
    }
    if (password.getText().toString().trim().equals("")
            || password.getText().length() != 12) {
        password.setError("Enter valid password");
        errorCount = errorCount + 1;
        if (errorCount == 1) {
            password.requestFocus();
        }
    } else {
        password.setError(null);
    }
    if (errorCount == 0) {
        return true;
    } else {
        return false;
    }

}

private void batteryTimer(){
    Timer timer = new Timer();
    TimerTask hourlyTask = new TimerTask() {
        @Override
        public void run() {
            if (networkUtil.isConnected()) {
                batteryLevelCheckService(); // **Getting Error at this Line**
            }
            else {
                offlineBatteryStatus();
            }
        }
    };
    timer.scheduleAtFixedRate(hourlyTask, 01, 60000);
}

private void batteryLevelCheckService() {
    OkHttpClient client = new OkHttpClient();
    String requestURL = String.format(getResources().getString(R.string.service_batteryLevelCheckService));

    JSONArray jsonArrayRequest = new JSONArray();
    JSONObject jsonRequest;
    try {
        List<BatteryStatusModel> batStatusOffline = new Select().from(BatteryStatusModel.class).execute();
        if (batStatusOffline.size() > 0) {
            for (BatteryStatusModel batStatusObject : batStatusOffline) {
                jsonRequest = new JSONObject();
                jsonRequest.accumulate("strTime", batStatusObject.batStatTime);
                jsonRequest.accumulate("batteryStatusLat", "" + batStatusObject.battery_lat);
                jsonRequest.accumulate("batteryStatusLog", "" + batStatusObject.battery_lon);
                jsonRequest.accumulate("empAuthKey", sharePref.getValueFromSharePref("authKey"));
                jsonRequest.accumulate("mobno", "" + sharePref.getValueFromSharePref("mobileno"));
                jsonRequest.accumulate("strBatteryStatus", "" + batStatusObject.batteryStatus);
                jsonArrayRequest.put(jsonRequest);
            }
        }

        Intent intent = this.registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));
        int level = intent.getIntExtra(BatteryManager.EXTRA_LEVEL, 0);
        int scale = intent.getIntExtra(BatteryManager.EXTRA_SCALE, 100);
        int percent = (level * 100) / scale;

        Date today = Calendar.getInstance().getTime();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        String time = simpleDateFormat.format(today);

        jsonRequest = new JSONObject();
        jsonRequest.accumulate("strTime", time);
        jsonRequest.accumulate("batteryStatusLat", "" + locationInfo.lastLat);
        jsonRequest.accumulate("batteryStatusLon", "" + locationInfo.lastLong);
        jsonRequest.accumulate("empAuthKey", sharePref.getValueFromSharePref("authKey"));
        jsonRequest.accumulate("mobNo", "" + sharePref.getValueFromSharePref("mobileno"));
        jsonRequest.accumulate("strBatteryStatus", "" + percent);
        jsonArrayRequest.put(jsonRequest);
    } catch (Exception e) {
        e.printStackTrace();
    }

    RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), jsonArrayRequest.toString());
    Request request = new Request.Builder()
            .url(requestURL) // Getting Error at this Line
            .post(body).build();

    client.newCall(request).enqueue(new Callback() {

        @Override
        public void onFailure(Call call, IOException e) {
        }

        @Override
        public void onResponse(Call call, Response response) throws IOException {
            if (response.isSuccessful()) {
                String responseString = response.body().string();
                try {
                    JSONObject jsonResponse = new JSONObject(responseString);
                    String status = jsonResponse.getString("status");
                    String message = jsonResponse.getString("message");
                    Log.d("jagteraho", "response :: status: " + status.toString() + " message: " + message);
                    if (status.equals("success")) {
                        new Delete().from(BatteryStatusModel.class).execute();
                    } else if (status.equals("failure")) {
                    } else if (status.equals("error")) {
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    });
}
java.lang.IllegalArgumentException: unexpected url: >http://192.168.2.20:8080/jagteraho/batteryStatus/save
at okhttp3.Request$Builder.url(Request.java:143)
at com.aspeage.jagteraho.LoginActivity.batteryLevelCheckService(LoginActivity.java:270)
at com.aspeage.jagteraho.LoginActivity.access$600(LoginActivity.java:59)
at com.aspeage.jagteraho.LoginActivity$4.run(LoginActivity.java:216)
at java.util.Timer$TimerImpl.run(Timer.java:284) 

请帮助我有可能的解决方案我对Android开发还很陌生

共有1个答案

咸疏珂
2023-03-14

您的错误来自OKHTTP。

如果您搜索错误消息,您可以看到OkHttp在哪里生成它:

https://github.com/square/okhttp/blob/master/okhttp/src/main/java/okhttp3/request.java#L142

  HttpUrl parsed = HttpUrl.parse(url);
  if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url);
  return url(parsed);
 类似资料:
  • 嗨,我使用Spring mvc 3 和Hibernate 4 开发一个 Web 应用程序。我正在为所有配置使用注释。 当我尝试选择一个实体时,出现错误: 如果我用< code >进行查询。create SQL query() hibernate返回一个< code>ClanMember实体的< code>Object[]实例。 道中的方法: 实体: 表 CLAN_MEMBERS: 我做错了什么?先

  • 注:与此问题类似,但不相同。 我有一个快速js节点网站(WIP),但我一直得到以下错误: 我的: 文件只是一个基本的jade文件,描述了一个简单的引导navbar,因此我将省略它。 : 现在,当我访问时,除了

  • 我能够使用cloudera提供的示例jar在alluxio上运行wordcount,使用: 但是当我使用附带代码创建的jar时,我不能运行它,这也是一个示例wordcount示例代码 上面的代码是使用maven pom.xml文件构建的 你能帮我在alluxio集群中运行我的wordcount程序吗。希望没有额外的配置添加到pom文件运行相同。

  • 我正在尝试使用React创建一个简单的网站,但由于某些原因,它只显示一个空白页面。 显示在控制台日志上的错误是未捕获的语法错误:意外令牌 非常感谢。 HTML: 主JS: Navbar: 身体

  • 我有一个索引php页面,其中包含一个打印数组值的代码

  • 问题内容: 我是Python的新手,正在收到此错误: 在这段代码上: 问题答案: 尽管缩进错误在StackOverflow页面上很明显,但它们可能不在您的编辑器中。您在这里混合使用不同的缩进类型,1、4和8个空格。根据PEP8,您应始终使用四个空格进行缩进。您还应该避免混用制表符和空格。 我还建议您尝试使用‘ ‘命令行选项 来运行脚本,以确定何时意外混合使用制表符和空格。当然,任何体面的编辑器都可