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

获取错误java.lang.IllegalArgumentException:意外url,java,android,okhttp3,illegalargumentexception,Java,Android,Okhttp3,Illegalargumentexception,您好,我正试图使用json解析将一些数据发送到服务器,但活动正在崩溃,导致 java.lang.IllegalArgumentException:意外的url 这是我的活动代码,我正在注释我得到错误的行 public class LoginActivity extends AppCompatActivity { **// Showing Error at this LIne** public Location location; private Button btnLogin; private

您好,我正试图使用json解析将一些数据发送到服务器,但活动正在崩溃,导致
java.lang.IllegalArgumentException:意外的url

这是我的活动代码,我正在注释我得到错误的行

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

请帮助我找到可能的解决方案我在Android开发方面是新手

您的错误来自OkHttp

如果搜索错误消息,可以看到OkHttp在何处生成错误消息:

这意味着您的URL无效。正如对您问题的评论所指出的:
>http://192.168.2.20:8080/jagteraho/batteryStatus/save
不是有效的URL


您需要删除
>

我不确定,但看起来这个“>”不应该出现在url中。它可能是由异常生成的,但如果不是,请在读取xml文件时查看它。据我所知,xml看起来像这样
testURL
,这意味着您应该将>从第一个url带到url@XtremeBaumer:感谢您的快速解决方案我在xml文件中错误地插入了两个“>”。谢谢你,没问题,伙计。发生在我们当中最好的人身上。非常感谢你让我纠正这个问题。
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) 
  HttpUrl parsed = HttpUrl.parse(url);
  if (parsed == null) throw new IllegalArgumentException("unexpected url: " + url);
  return url(parsed);