Reputation: 254
I'm trying to make a http request from a page, but my application aways crash. This is my first Java application so I'm a beginner. I been researching for a while but could not find a solution:
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﹕ FATAL EXCEPTION: main Process: 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) at com.lookingunique.splashstockcontrol.MainActivity$1$1.onResponse(MainActivity.java:43) at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:60) at com.android.volley.toolbox.StringRequest.deliverResponse(StringRequest.java:30) at com.android.volley.ExecutorDelivery$ResponseDeliveryRunnable.run(ExecutorDelivery.java:99) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Upvotes: 0
Views: 1373
Reputation: 1647
It seems the request is not failing but the response doesn't contain a 100 length string if you check the documentation for substring it states that:
Throws:
IndexOutOfBoundsException - if the beginIndex is negative, or endIndex is larger than the length of this String object, or beginIndex is larger than endIndex.
Try and just print all the string instead
Upvotes: 3