我正在尝试从页面发出http请求,但是我的应用程序崩溃了。这是我的第一个Java应用程序,所以我是一个初学者。我研究了一段时间,但找不到解决方案:
package com.lookingunique.splashstockcontrol;
import android.app.Activity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
public class MainActivity extends Activity {
EditText barcode;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
barcode = (EditText) findViewById(R.id.etbarcode);
final Button addBtn = (Button) findViewById(R.id.button);
addBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String barcodeval = barcode.getText().toString();
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
String url ="http://xxzx.com/zzz/xzxx/yyy.php?barcode="+ barcodeval +"&action=check";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest( Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
String test = response.substring(0,100);
Toast.makeText(getApplicationContext(), test, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String test = "That didn't work!";
Toast.makeText(getApplicationContext(), test, Toast.LENGTH_SHORT).show();
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
}
});
barcode.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
addBtn.setEnabled(!barcode.getText().toString().trim().isEmpty());
}
@Override
public void afterTextChanged(Editable s) {
}
});
}
}
08-25 10:15:15.336 2029-2029 / com.lookingunique.splashstockcontrol E / AndroidRuntime致命异常:主进程:com.lookingunique.splashstockcontrol,PID:2029 java.lang.StringIndexOutOfBoundsException:length = 0; regionStart = 0; regionLength = 100 at java.lang.String.startEndAndLength(String.java:298)at java.lang.String.substring(String.java:1087)at com.lookingunique.splashstockcontrol.MainActivity $ 1 $ 1.onResponse(MainActivity.java: 47)在com.lookingunique.splashstockcontrol.MainActivity $ 1 $ 1.onResponse(MainActivity.java:43)在com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60)在com.android.volley.toolbox.StringRequest com.android.volley.ExecutorDelivery $ ResponseDeliveryRunnable.run(ExecutorDelivery.java:99)的android.os.Handler.handleCallback(Handler.java:739)上的.deliverResponse(StringRequest.java:30)。
似乎请求没有失败,但是如果您查看文档中的子字符串,则响应不包含100长度的字符串,它指出:
Throws: IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
尝试只打印所有字符串
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句