Java 显示json到文本视图强制关闭
我想通过php显示mysql数据库中的产品详细信息,并在android textview中显示。场景如下:单击产品列表时。它将产品id传递给名为productdetail的新意图。该id将用于通过php获取mysql db中的详细产品。PHP文件已成功返回json格式的数据。但当我运行程序时,它会在打开productdetail时强制关闭。我在这个方法中发现的问题是new GetProductDetails().execute(); 这是我的密码: getproductdetail.java 公共类ProductDetail扩展了活动{Java 显示json到文本视图强制关闭,java,php,android,json,Java,Php,Android,Json,我想通过php显示mysql数据库中的产品详细信息,并在android textview中显示。场景如下:单击产品列表时。它将产品id传递给名为productdetail的新意图。该id将用于通过php获取mysql db中的详细产品。PHP文件已成功返回json格式的数据。但当我运行程序时,它会在打开productdetail时强制关闭。我在这个方法中发现的问题是new GetProductDetails().execute(); 这是我的密码: getproductdetail.java 公
TextView txtName;
TextView txtVersion;
TextView txtDesc;
String pid;
//progress dialog
private ProgressDialog pDialog;
//Creating JSON Parser object
JSONParser jsonParser = new JSONParser();
//url to get all products list
private static String url_product_details = "http://visioinformatika.com/demo/appswitcher/bin/get_product_details.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_PRODUCTS = "products";
private static final String TAG_PID = "pid";
private static final String TAG_NAME = "product_name";
private static final String TAG_VERSION = "app_version";
private static final String TAG_PDETAIL = "product_detail";
// products JSONArray
JSONArray products = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_detail);
// getting product details from intent
Intent i = getIntent();
// getting product id (pid) from intent
pid = i.getStringExtra(TAG_PID);
//txtName = (TextView) findViewById(R.id.app_name);
//txtName.setText(pid);
// Getting complete product details in background thread
new GetProductDetails().execute();
}
/**
* Background Async Task to Get complete product details
* */
class GetProductDetails extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(ProductDetail.this);
pDialog.setMessage("Loading product details. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(true);
pDialog.show();
}
/**
* Getting product details in background thread
* */
protected String doInBackground(String... params) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
// Check for success tag
int success;
try {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("pid", pid));
// getting product details by making HTTP request
// Note that product details url will use GET request
JSONObject json = jsonParser.makeHttpRequest(
url_product_details, "GET", params);
// check your log for json response
Log.d("Single Product Details", json.toString());
// json success tag
success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// successfully received product details
JSONArray productObj = json
.getJSONArray(TAG_PRODUCTS); // JSON Array
// get first product object from JSON Array
JSONObject product = productObj.getJSONObject(0);
// product with this pid found
// Edit Text
txtName = (TextView) findViewById(R.id.app_name);
txtVersion = (TextView) findViewById(R.id.app_version);
txtDesc = (TextView) findViewById(R.id.desc_app);
// display product data in TextView
txtName.setText(product.getString(TAG_NAME));
txtVersion.setText(product.getString(TAG_VERSION));
txtDesc.setText(product.getString(TAG_PDETAIL));
}else{
// product with pid not found
}
} catch (JSONException e) {
e.printStackTrace();
}
}
});
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
// dismiss the dialog once got all details
pDialog.dismiss();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.product_detail, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
这是我的事故记录
10-15 09:50:32.930:W/dalvikvm(19033):threadid=1:线程以未捕获异常退出(组=0x41534ba8)
10-15 09:50:32.930:E/AndroidRuntime(19033):致命异常:main
10-15 09:50:32.930:E/AndroidRuntime(19033):进程:com.visioninformatika.appswitcher,PID:19033
10-15 09:50:32.930:E/AndroidRuntime(19033):android.os.NetworkOnMainThreadException
10-15 09:50:32.930:E/AndroidRuntime(19033):在android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1145)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于java.net.InetAddress.lookupHostByName(InetAddress.java:385)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于java.net.InetAddress.getAllByName(InetAddress.java:214)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于com.visioninformatika.appswitcher.JSONParser.makeHttpRequest(JSONParser.java:62)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于com.visioninformatika.appswitcher.ProductDetail$GetProductDetails$1.run(ProductDetail.java:102)
10-15 09:50:32.930:E/AndroidRuntime(19033):在android.os.Handler.handleCallback(Handler.java:733)上
10-15 09:50:32.930:E/AndroidRuntime(19033):在android.os.Handler.dispatchMessage(Handler.java:95)上
10-15 09:50:32.930:E/AndroidRuntime(19033):在android.os.Looper.loop(Looper.java:136)
10-15 09:50:32.930:E/AndroidRuntime(19033):在android.app.ActivityThread.main(ActivityThread.java:5001)上
10-15 09:50:32.930:E/AndroidRuntime(19033):位于java.lang.reflect.Method.Invokenactive(本机方法)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于java.lang.reflect.Method.invoke(Method.java:515)
10-15 09:50:32.930:E/AndroidRuntime(19033):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
10-15 09:50:32.930:E/AndroidRuntime(19033):位于com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
10-15 09:50:32.930:E/AndroidRuntime(19033):在dalvik.system.NativeStart.main(本机方法)
对不起,我英语不好。我是android新手:)请将此代码放入postexecute方法。UI无法填充主线程
// display product data in TextView
txtName.setText(product.getString(TAG_NAME));
txtVersion.setText(product.getString(TAG_VERSION));
txtDesc.setText(product.getString(TAG_PDETAIL));
把这行放在OnCreate()中,就像RobinHood说的
txtName = (TextView) findViewById(R.id.app_name);
txtVersion = (TextView) findViewById(R.id.app_version);
txtDesc = (TextView) findViewById(R.id.desc_app);
您必须改进您的编码风格,在onCreate中初始化视图 如果 您希望在后台更新UI,然后继续 否则
在doInBackground中进行后台处理,最后更新asynTask的onPostExecute中的视图。将此代码放入onPreExecute()中,如下所示
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// product with this pid found
// Edit Text
txtName = (TextView) findViewById(R.id.app_name);
txtVersion = (TextView) findViewById(R.id.app_version);
txtDesc = (TextView) findViewById(R.id.desc_app);
pDialog.show();
}
我已经把代码放在我的post execute中了。但是仍然得到错误警告。对不起,…我是android新手,对java编程知之甚少。我已经把代码放在我的post execute方法中了。但是这是显示错误“产品无法解决”,谢谢你的建议robbin:),我已经初始化了textView。但是我不明白如何在PostExecute上更新视图…你能详细解释一下吗robbin?你认为
runOnUiThread()
在你的异步任务中有什么作用,以及它与NetworkOnMainThreadException
有什么关系?
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_detail);
txtName = (TextView) findViewById(R.id.app_name);
txtVersion = (TextView) findViewById(R.id.app_version);
txtDesc = (TextView) findViewById(R.id.desc_app);
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
// product with this pid found
// Edit Text
txtName = (TextView) findViewById(R.id.app_name);
txtVersion = (TextView) findViewById(R.id.app_version);
txtDesc = (TextView) findViewById(R.id.desc_app);
pDialog.show();
}