Reputation: 1733
I am new to android programming, and I want to create a very simple app that takes a String
in EditText
and when I click a button
, it takes the String
and sends it to my localhost
to save it in my database.
But it is giving a run time error for the doInBackground()
, and I don't understand why. Please, help me.
Here is my Log :
07-28 12:27:21.591: E/AndroidRuntime(2267): FATAL EXCEPTION: AsyncTask #1
07-28 12:27:21.591: E/AndroidRuntime(2267): Process: com.appeight.jsontest, PID: 2267
07-28 12:27:21.591: E/AndroidRuntime(2267): java.lang.RuntimeException: An error occured while executing doInBackground()
07-28 12:27:21.591: E/AndroidRuntime(2267): at android.os.AsyncTask$3.done(AsyncTask.java:300)
07-28 12:27:21.591: E/AndroidRuntime(2267): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
07-28 12:27:21.591: E/AndroidRuntime(2267): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
07-28 12:27:21.591: E/AndroidRuntime(2267): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
07-28 12:27:21.591: E/AndroidRuntime(2267): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
07-28 12:27:21.591: E/AndroidRuntime(2267): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
07-28 12:27:21.591: E/AndroidRuntime(2267): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
07-28 12:27:21.591: E/AndroidRuntime(2267): at java.lang.Thread.run(Thread.java:841)
07-28 12:27:21.591: E/AndroidRuntime(2267): Caused by: java.lang.StringIndexOutOfBoundsException: length=0; index=3
07-28 12:27:21.591: E/AndroidRuntime(2267): at java.lang.String.indexAndLength(String.java:584)
07-28 12:27:21.591: E/AndroidRuntime(2267): at java.lang.String.substring(String.java:1449)
07-28 12:27:21.591: E/AndroidRuntime(2267): at com.appeight.jsontest.JSONParser.makeHttpRequest(JSONParser.java:79)
07-28 12:27:21.591: E/AndroidRuntime(2267): at com.appeight.jsontest.AddScreen$CreateNewProduct.doInBackground(AddScreen.java:50)
07-28 12:27:21.591: E/AndroidRuntime(2267): at com.appeight.jsontest.AddScreen$CreateNewProduct.doInBackground(AddScreen.java:1)
07-28 12:27:21.591: E/AndroidRuntime(2267): at android.os.AsyncTask$2.call(AsyncTask.java:288)
07-28 12:27:21.591: E/AndroidRuntime(2267): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
07-28 12:27:21.591: E/AndroidRuntime(2267): ... 4 more
My code is this :
package com.appeight.jsontest;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class AddScreen extends ActionBarActivity {
EditText tv;
JSONParser jsonParser = new JSONParser();
String name;
private static String add_name = "http://10.0.2.2/android_test/add.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_screen);
tv = (EditText) findViewById(R.id.editText1);
}
public void addToDb(View vvv)
{
new CreateNewProduct().execute();
}
class CreateNewProduct extends AsyncTask<String, String, String>
{
protected String doInBackground(String... args)
{
name = tv.getText().toString();
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
// getting JSON Object
// Note that add name URL accepts POST method
JSONObject json = jsonParser.makeHttpRequest(add_name,
"POST", params);
return null;
}
protected void onPostExecute(String result)
{
Toast.makeText(getBaseContext(), "Successful", Toast.LENGTH_SHORT).show();
}
}
}
Someone, please help, I am really stuck bad here, and I am a newbie, so please pardon me for any silly mistakes.
Upvotes: 0
Views: 779
Reputation: 45490
You have few mistakes here:
doInBackground
you are not supposed to access the UIdoInBackground
when postExecute
expect a String result.here is a clean way to do the same thing
public void addToDb(View vvv)
{
//Get the value from the textView
name = tv.getText().toString();
//build Params
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("name", name));
CreateNewProduct productTask = new CreateNewProduct(params);
productTask.execute();
}
class CreateNewProduct extends AsyncTask<String, String, JSONObject>
{
List<NameValuePair> params;
public CreateNewProduct(List<NameValuePair> params){
this.params = params;
}
protected JSONObject doInBackground(String... args)
{
JSONObject json = jsonParser.makeHttpRequest(add_name,"POST", this.params);
return json;
}
protected void onPostExecute(JSONObject result)
{
//this assumes that the response looks like this:
//{"success" : true }
boolean success = result.getBoolean("success");
Toast.makeText(getBaseContext(), success ? "Successful" : "Failure", Toast.LENGTH_SHORT).show();
}
}
Upvotes: 1
Reputation: 2937
A string of length 0 is accessed by your com.appeight.jsontest.JSONParser object, but it is trying to access index 3. Is this a class you wrote or a library? If a class you wrote, try to find out why (check line code in it).
07-28 12:27:21.591: E/AndroidRuntime(2267): Caused by: java.lang.StringIndexOutOfBoundsException: length=0; index=3
07-28 12:27:21.591: E/AndroidRuntime(2267): at java.lang.String.indexAndLength(String.java:584)
07-28 12:27:21.591: E/AndroidRuntime(2267): at java.lang.String.substring(String.java:1449)
07-28 12:27:21.591: E/AndroidRuntime(2267): at com.appeight.jsontest.JSONParser.makeHttpRequest(JSONParser.java:79)
Upvotes: 0
Reputation: 383
Surround the contents of doInBackground with try and catch the exception, and also do not forget to dismiss the dialog using the command: pDialog.dismiss(); in onPostExecute()
Upvotes: 0