I create a program from Android Hive
I get some error on this:
protected String doInBackground(String... args) {
String id = id_anggota.getText().toString();
String nama = nama_anggota.getText().toString();
String kelamin = jenis_kelamin.getText().toString();
String kls = kelas.getText().toString();
String tanggal = tanggal_daftar.getText().toString();
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id_anggota", id));
params.add(new BasicNameValuePair("nama_anggota", nama));
params.add(new BasicNameValuePair("jenis_kelamin", kelamin));
params.add(new BasicNameValuePair("kelas", kls));
params.add(new BasicNameValuePair("tanggal_daftar", tanggal));
JSONObject json = jsonParser.makeHttpRequest(url_create_product,
"POST", params);
I get 2 kind of error:
id_anggota.getText()
must be called from the UI Thread.makeHttpRequest
method cannot resolved.What solutions can I do for this problem?
Update:
I use some library:
Inside your AsyncTask
please declare some variables:
private String id ;
private String nama;
private String kelamin ;
private String kls;
private String tanggal;
Then on onPreExecute()
you can set values for them:
@Override
protected void onPreExecute(Void result) {
super.onPostExecute(result);
id = id_anggota.getText().toString();
nama = nama_anggota.getText().toString();
kelamin = jenis_kelamin.getText().toString();
kls = kelas.getText().toString();
tanggal = tanggal_daftar.getText().toString();
}
Finally on doInBackground()
:
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id_anggota", id));
params.add(new BasicNameValuePair("nama_anggota", nama));
params.add(new BasicNameValuePair("jenis_kelamin", kelamin));
params.add(new BasicNameValuePair("kelas", kls));
params.add(new BasicNameValuePair("tanggal_daftar", tanggal));
makeHttpRequest
can be found in JSONParser
class here or here
Move
String id = id_anggota.getText().toString();
String nama = nama_anggota.getText().toString();
String kelamin = jenis_kelamin.getText().toString();
String kls = kelas.getText().toString();
String tanggal = tanggal_daftar.getText().toString();
before yourAsyncTask.execute()
.
Change yourAsyncTask.execute()
to yourAsyncTask.execute(id,nama,kelamin,kls,tanggal)
.
And in method doInBackground()
protected String doInBackground(String... args) {
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("id_anggota", args[0]));
params.add(new BasicNameValuePair("nama_anggota", args[1]));
params.add(new BasicNameValuePair("jenis_kelamin", args[2]));
params.add(new BasicNameValuePair("kelas", args[3]));
params.add(new BasicNameValuePair("tanggal_daftar", args[4]));
}
String id = id_anggota.getText().toString();
String nama = nama_anggota.getText().toString();
String kelamin = jenis_kelamin.getText().toString();
String kls = kelas.getText().toString();
String tanggal = tanggal_daftar.getText().toString();
This getText()
interacts with the view, a TextView
and all the views in android OS can only work in the UI(User Interface thread). doInBackground()
makes a separate thread, a background thread like the name suggests. So getting the text from the TextView
should always be from the UI thread which is managed by the android OS. So move the above code inside onPreExecute()
which runs on the UI thread.
As far as makeHttpRequest()
is concerned, just open the jar file and see if its has that makeHttpRequest()
or not. You can decompile a jar file in the command prompt like this.