Php 致命异常:AsyncTask#1执行doInBackground时出错
请帮帮我, 我想做一个条形码扫描仪,并将结果发送到我的数据库中,但我有错误 这是我的代码: MainActivity.javaPhp 致命异常:AsyncTask#1执行doInBackground时出错,php,android,android-asynctask,Php,Android,Android Asynctask,请帮帮我, 我想做一个条形码扫描仪,并将结果发送到我的数据库中,但我有错误 这是我的代码: MainActivity.java package common.TER2015; import java.io.IOException; import java.util.ArrayList; import org.apache.http.NameValuePair; import org.apache.http.client.ClientProtocolException; import
package common.TER2015;
import java.io.IOException;
import java.util.ArrayList;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import youb.y.com.android.zxinglib.integrator.IntentIntegrator;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.TextView;
import android.widget.Toast;
import dossier.android.ter.R;
public class MainActivity extends Activity {
private Handler handler = new Handler();
private TextView txtScanResult;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtScanResult = (TextView) findViewById(R.id.scan_result);
View btnScan = findViewById(R.id.scan_button);
// le boutton Scanner
btnScan.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// dernièer paramètre se met à true pr la lumiére
IntentIntegrator.initiateScan(MainActivity.this, R.layout.capture,
R.id.viewfinder_view, R.id.preview_view, true);
Insert task = new Insert();
task.execute(new String[]{"http://10.0.2.2:80/codebarre/insert.php"});
}
});
}
private class Insert extends AsyncTask<String, Void, Boolean>{
ProgressDialog dialog = new ProgressDialog(MainActivity.this);
@Override
protected void onPreExecute() {
dialog.setMessage("Envoi en cours..");
dialog.show();
}
@Override
protected Boolean doInBackground(String... urls) {
for(String url : urls){
try {
ArrayList<NameValuePair> pairs = new ArrayList<NameValuePair>();
pairs.add(new BasicNameValuePair("codebarre", txtScanResult.getText().toString()));
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(url);
post.setEntity(new UrlEncodedFormEntity(pairs));
client.execute(post);
} catch (ClientProtocolException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
return false;
}
catch (IOException e) {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
return false;
}
}
return true;
}
@Override
protected void onPostExecute(Boolean result) {
if(result == true ){
Toast.makeText(MainActivity.this, "Succée", Toast.LENGTH_SHORT).show(); } else{
Toast.makeText(MainActivity.this, "Echéq", Toast.LENGTH_SHORT).show();
}
dialog.dismiss();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case IntentIntegrator.REQUEST_CODE:
jim.h.common.android.zxinglib.integrator.IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult == null) {
return;
}
final String result = scanResult.getContents();
if (result != null) {
handler.post(new Runnable() {
@Override
public void run() {
// txtScanResult.setText(result);
}
}
);
}
break;
default:
}}}
package common.TER2015;
导入java.io.IOException;
导入java.util.ArrayList;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.ClientProtocolException;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入youb.y.com.android.zxinglib.integrator.integrator;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.content.Intent;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.os.Handler;
导入android.view.view;
导入android.view.view.OnClickListener;
导入android.widget.TextView;
导入android.widget.Toast;
导入docsier.android.ter.R;
公共类MainActivity扩展了活动{
私有处理程序=新处理程序();
私有文本视图txtScanResult;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtScanResult=(TextView)findViewById(R.id.scan\u结果);
查看btnScan=findviewbyd(R.id.scan_按钮);
//布顿扫描仪
setOnClickListener(新的OnClickListener(){
@凌驾
公共void onClick(视图v){
//德尔尼耶·帕拉梅遇到了一个真正的情人
监视整合器。启动扫描(MainActivity.this,R.layout.capture,
R.id.取景器视图,R.id.预览视图,真);
插入任务=新插入();
task.execute(新字符串[]{”http://10.0.2.2:80/codebarre/insert.php"});
}
});
}
私有类插入扩展异步任务{
ProgressDialog=新建ProgressDialog(MainActivity.this);
@凌驾
受保护的void onPreExecute(){
对话框.setMessage(“Envoi en cours..”;
dialog.show();
}
@凌驾
受保护的布尔doInBackground(字符串…URL){
for(字符串url:url){
试一试{
ArrayList pairs=新的ArrayList();
add(新的BasicNameValuePair(“codebarre”,txtScanResult.getText().toString());
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(url);
setEntity(新的UrlEncodedFormEntity(对));
客户。执行(post);
}捕获(客户端协议例外e){
Toast.makeText(MainActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
返回false;
}
捕获(IOE异常){
Toast.makeText(MainActivity.this,e.toString(),Toast.LENGTH_SHORT).show();
返回false;
}
}
返回true;
}
@凌驾
受保护的void onPostExecute(布尔结果){
如果(结果==真){
Toast.makeText(MainActivity.this,“succee”,Toast.LENGTH_SHORT.show();}else{
Toast.makeText(MainActivity.this,“Echéq”,Toast.LENGTH_SHORT.show();
}
dialog.dismise();
}
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
开关(请求代码){
案例监管者请求代码:
jim.h.common.android.zxinglib.integrator.IntentResult scanResult=IntentIntegrator.parseActivityResult(请求代码、结果代码、数据);
if(scanResult==null){
返回;
}
最终字符串结果=scanResult.getContents();
如果(结果!=null){
handler.post(新的Runnable(){
@凌驾
公开募捐{
//txtScanResult.setText(结果);
}
}
);
}
打破
违约:
}}}
Main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="@string/scan_resut_lable"
android:textColor="#000" />
<TextView
android:id="@+id/scan_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/scan_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|top"
android:text="@string/scan" />
<ImageView
android:id="@drawable/upecnoir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:src="@drawable/upecnoir" />
</LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<SurfaceView android:id="@+id/preview_view"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_centerInParent="true" />
<jim.h.common.android.zxinglib.view.ViewfinderView
android:id="@+id/viewfinder_view" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#00000000" />
<TextView android:id="@+id/status_view" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal"
android:background="#00000000" android:text="@string/msg_default_status"
android:textColor="#ffffffff" android:textSize="14sp" />
</FrameLayout>
Capture.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@android:color/white"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="80dp"
android:text="@string/scan_resut_lable"
android:textColor="#000" />
<TextView
android:id="@+id/scan_result"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/scan_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|top"
android:text="@string/scan" />
<ImageView
android:id="@drawable/upecnoir"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:src="@drawable/upecnoir" />
</LinearLayout>
<?xml version="1.0" encoding="UTF-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent">
<SurfaceView android:id="@+id/preview_view"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:layout_centerInParent="true" />
<jim.h.common.android.zxinglib.view.ViewfinderView
android:id="@+id/viewfinder_view" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#00000000" />
<TextView android:id="@+id/status_view" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_gravity="bottom|center_horizontal"
android:background="#00000000" android:text="@string/msg_default_status"
android:textColor="#ffffffff" android:textSize="14sp" />
</FrameLayout>
insert.php
<?php
//if (isset($_POST['codebr'])){
include_once("connection.php");
$codebarre = $_POST['codebarre'];
mysql_query("insert into code(codeb) values('$codebarre')");
?>
<html>
<body>
<form action="insert.php" method="post">
codebarre <input type="text" name="codebarre" /> <br/>
<input type="submit" value ="Insert" />
</form>
</body>
</html>
您正在尝试从AsyncTask中的doInBackground()
方法更新try-catch块中的UI(显示Toast消息)。你不能那样做。如果要显示来自doInBackground()的Toast消息,可以使用runOnUiThread
方法。例如:
catch (ClientProtocolException e) {
runOnUiThread(new Runnable() {
public void run() {
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
return false;
}
});
}
谢谢“你真是太棒了:)