gpt4 book ai didi

java - 无法从 onPostExecute() 的 doInBackGround() 进程获取值

转载 作者:行者123 更新时间:2023-12-01 19:12:48 25 4
gpt4 key购买 nike

我正在使用 AsyncTask 从数据库插入、更新和删除数据。我用这段代码来插入、更新、删除,它工作得很好。但是当我想使用 select 并在 EditText 处显示数据时,我无法从 doInBackground() 获取值到 onPostExecute() 并且什么也没显示。

这是我的代码:

MenuUtama.java

public class MenuUtama extends Activity {

/** Called when the activity is first created. */
private TextView nama_user;
private String nm_user = "";
private EditText kode, nama, harga, deskripsi;
private Button insert, update, delete, cek;
private String kode1, nama1, harga1, deskripsi1;
JSONArray data = null;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);

kode = (EditText) findViewById(R.id.editKode);
nama = (EditText) findViewById(R.id.editNama);
harga = (EditText) findViewById(R.id.editHarga);
deskripsi = (EditText) findViewById(R.id.editDes);
cek = (Button) findViewById(R.id.btnCek);
insert = (Button) findViewById(R.id.buttonInsert);
update = (Button) findViewById(R.id.buttonUpdate);
delete = (Button) findViewById(R.id.buttonDelete);
nama_user = (TextView) findViewById(R.id.textView3);

Intent i = getIntent();
nm_user = i.getStringExtra("nama_user");
nama_user.setText(nm_user);


insert.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String url = "";
url = "http://192.168.1.10/crudsederhana/aksi.php";

try {
String ko = URLEncoder.encode(kode.getText().toString(),"utf-8");
String n = URLEncoder.encode(nama.getText().toString(),"utf-8");
String hr = URLEncoder.encode(harga.getText().toString(),"utf-8");
String d = URLEncoder.encode(deskripsi.getText().toString(), "utf-8");

url += "?a=insert&kd=" + ko + "&nm=" + n + "&hrg=" + hr + "&deskripsi=" + d;

new CRUD().execute(url);
}

catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});

update.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
String url = "";

url = "http://192.168.1.10/crudsederhana/aksi.php";

try {
String ko = URLEncoder.encode(kode.getText().toString(),"utf-8");
String n = URLEncoder.encode(nama.getText().toString(),"utf-8");
String hr = URLEncoder.encode(harga.getText().toString(),"utf-8");
String d = URLEncoder.encode(deskripsi.getText().toString(), "utf-8");

url += "?a=update&kd=" + ko + "&nm=" + n + "&hrg=" +hr+ "&des=" + d;

new CRUD().execute(url);
}

catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
});

delete.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String url = "";
kode1 = kode.getText().toString();
url = "http://192.168.1.10/crudsederhana/aksi.php?a=delete&kd=" + kode1;

new CRUD().execute(url);
}
});

cek.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String url = "";
kode1 = kode.getText().toString();
url = "http://192.168.1.10/crudsederhana/aksi.php?a=read&kd="+kode1;

new CRUD().execute(url);
}
});
}

public class CRUD extends AsyncTask<String, String, String> {
String success;
String kode_d, nama_d, harga_d, des_d;

@Override
protected void onPreExecute() {
super.onPreExecute();
}

@Override
protected String doInBackground(String... params) {
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(params[0]);

try {
success = json.getString("success");
Log.e("error", "nilai sukses=" + success);
JSONArray hasil = json.getJSONArray("login");

if (success.equals("1")) {
for (int i = 0; i < hasil.length(); i++) {
JSONObject c = hasil.getJSONObject(i);

kode_d = c.getString("kd");
nama_d = c.getString("nm");
harga_d = c.getString("hrg");
des_d = c.getString("deskripsi");
}
}

else {
Log.e("erro", "tidak bisa ambil data 0");
}
}

catch (Exception e) {
// TODO: handle exception
Log.e("erro", "tidak bisa ambil data 1");
}

return kode_d;
}

protected void onPostExecute(String result) {
super.onPostExecute(result);

kode.setText(kode_d);
nama.setText(nama_d);
harga.setText(harga_d);
deskripsi.setText(des_d);
}
}
}

JSONParser.java

public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";

// constructor
public JSONParser() {
}

public JSONObject getJSONFromUrl(String url) {
// Making HTTP request
try {

// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}

catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

catch (ClientProtocolException e) {
e.printStackTrace();
}

catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);

StringBuilder sb = new StringBuilder();
String line = null;

while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}

is.close();
json = sb.toString();
}

catch (Exception e) {
Log.e("Buffer Error", "Error converting result " +e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
}

catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " +e.toString());
}

// return JSON String
return jObj;
}

public JSONObject makeHttpRequest(String url, String method, List<NameValuePair> params) {

// Making HTTP request
try {
// check for request method
if (method == "POST") {

// request method is POST
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}

else if (method == "GET") {
// request method is GET
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
}

catch (UnsupportedEncodingException e) {
e.printStackTrace();
}

catch (ClientProtocolException e) {
e.printStackTrace();
}

catch (IOException e) {
e.printStackTrace();
}

try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;

while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}

is.close();
json = sb.toString();
}

catch (Exception e) {
Log.e("Buffer Error", "Error converting result " +e.toString());
}

// try parse the string to a JSON object
try {
jObj = new JSONObject(json);
}

catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " +e.toString());
}

// return JSON String
return jObj;
}
}

最佳答案

这是因为您没有从 doInBackground() 返回任何值(始终返回 null),并且您没有在 onPostExecute(String result)< 中使用“字符串” 形式参数。垃圾进垃圾出

关于java - 无法从 onPostExecute() 的 doInBackGround() 进程获取值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59456715/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com