Java Android-使用主线程并将数据发布到PHP的异常
当我运行android应用程序并激活一个click事件,尝试将数据从表行传输到PHP文件时,主线程会强制应用程序关闭,而不管是否在后台使用另一个线程来建立网络连接(使用AsyncTask)。以下是我提供的文件: MainActivity.javaJava Android-使用主线程并将数据发布到PHP的异常,java,php,android,multithreading,android-asynctask,Java,Php,Android,Multithreading,Android Asynctask,当我运行android应用程序并激活一个click事件,尝试将数据从表行传输到PHP文件时,主线程会强制应用程序关闭,而不管是否在后台使用另一个线程来建立网络连接(使用AsyncTask)。以下是我提供的文件: MainActivity.java package com.example.stevenkennedy.categorizer; import android.content.Intent; import android.graphics.Bitmap; import android.
package com.example.stevenkennedy.categorizer;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
public class MainActivity extends AppCompatActivity {
// Local Variables
private Boolean snapBool = false;
private Boolean snapBool2 = false;
private Boolean snapBool3 = false;
private Boolean snapBool4 = false;
private Boolean snapBool5 = false;
// Add ImageView objects for camera
private ImageView snap;
private ImageView snap2;
private ImageView snap3;
private ImageView snap4;
private ImageView snap5;
private static final int CAMERA_REQUEST = 1888;
// Add Button for click event for server communication
private Button sendBtn;
// Row 1
private EditText idRow1;
private EditText nameRow1;
private Spinner categoryRow1;
private EditText commentRow1;
private EditText priceRow1;
private ImageView galleryRow1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Row 1
idRow1 = (EditText) findViewById(R.id.idBox);
nameRow1 = (EditText) findViewById(R.id.nameBox);
categoryRow1 = (Spinner) findViewById(R.id.cateBox);
commentRow1 = (EditText) findViewById(R.id.commBox);
priceRow1 = (EditText) findViewById(R.id.priceBox);
galleryRow1 = (ImageView) findViewById(R.id.gallBox);
// listeners for camera event
snap = (ImageView) findViewById(R.id.gallBox);
snap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
// flag local boolean
snapBool = true;
}
});
snap2 = (ImageView) findViewById(R.id.gallBox2);
snap2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
// flag local boolean
snapBool2 = true;
}
});
snap3 = (ImageView) findViewById(R.id.gallBox3);
snap3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
// flag local boolean
snapBool3 = true;
}
});
snap4 = (ImageView) findViewById(R.id.gallBox4);
snap4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
// flag local boolean
snapBool4 = true;
}
});
snap5 = (ImageView) findViewById(R.id.gallBox5);
snap5.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
// flag local boolean
snapBool5 = true;
}
});
// confirm button to send data to server
sendBtn = (Button) findViewById(R.id.send);
sendBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// create AsyncTask to get user entered cell data
new MyTask().execute();
}
});
} // ******** ON CREATE END ************
/*
* Method that activates the camera and gets a thumbnail of the photo for the
* table cell
*/
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
if (snapBool == true) {
snap.setImageBitmap(photo);
} else if (snapBool2 == true) {
snap2.setImageBitmap(photo);
} else if (snapBool3 == true) {
snap3.setImageBitmap(photo);
} else if (snapBool4 == true) {
snap4.setImageBitmap(photo);
} else if(snapBool5 == true) {
snap5.setImageBitmap(photo);
}
// set boolean flags back to false
snapBool = false;
snapBool2 = false;
snapBool3 = false;
snapBool4 = false;
snapBool5 = false;
}
}
/*
* Method to get cell data of Row 1 and send it to a sql server using
* post data with php
*/
public void getCells() {
// get the data in boxes
String str1 = idRow1.getText().toString();
String str2 = nameRow1.getText().toString();
String str3 = categoryRow1.getSelectedItem().toString();
String str4 = commentRow1.getText().toString();
String str5 = priceRow1.getText().toString();
//galleryRow1.get
// create sql object
MySQLConnect sql = new MySQLConnect(str1, str2, str3, str4, str5, "png23.png");
sql.sendTo();
}
/**
* AsyncTask Class
*/
private class MyTask extends AsyncTask<Void, Void, Void> {
@Override
protected Void doInBackground(Void... params) {
getCells();
return null;
}
protected void onPostExecute(String result) {}
@Override
protected void onPreExecute() {}
@Override
protected void onProgressUpdate(Void... values) {}
}
} // ************** CLASS END **************
package com.example.stevenkennedy.categorizer;
import android.media.Image;
import android.os.AsyncTask;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
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;
/**
* Created by Steven Kennedy on 27/10/2015.
*/
public class MySQLConnect {
// Class Variables
private String id;
private String name;
private String cate;
private String comm;
private String price;
private String gall;
// constructor
public MySQLConnect(String id, String name, String cate, String comm, String price, String gall){
this.id = id;
this.name = name;
this.cate = cate;
this.comm = comm;
this.price = price;
this.gall = gall;
}
// getters and setters
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCate() {
return cate;
}
public void setCate(String cate) {
this.cate = cate;
}
public String getComm() {
return comm;
}
public void setComm(String comm) {
this.comm = comm;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getGall() {
return gall;
}
public void setGall(String gall) {
this.gall = gall;
}
public void sendTo() {
// HTTP client
HttpClient client = new DefaultHttpClient();
// use php to post data to
HttpPost post = new HttpPost("http://myPHPfile.com/myFile.php");
try {
// add to arraylist
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// add values
nameValuePairs.add(new BasicNameValuePair("id", this.getId() ));
nameValuePairs.add(new BasicNameValuePair("name", this.getName()));
nameValuePairs.add(new BasicNameValuePair("category", this.getCate()));
nameValuePairs.add(new BasicNameValuePair("comment", this.getComm()));
nameValuePairs.add(new BasicNameValuePair("price", this.getPrice()));
//nameValuePairs.add(new BasicNameValuePair("gallery", this.getGall()));
// send to server - POST
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//HttpResponse response = client.execute(post);
client.execute(post);
} catch (ClientProtocolException ed) {
// TODO Auto-generated catch block
ed.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
} // ********* END ***********
package com.example.stevenkennedy.categorizer;
导入android.content.Intent;
导入android.graphics.Bitmap;
导入android.os.AsyncTask;
导入android.support.v7.app.AppActivity;
导入android.os.Bundle;
导入android.view.view;
导入android.widget.Button;
导入android.widget.EditText;
导入android.widget.ImageView;
导入android.widget.Spinner;
公共类MainActivity扩展了AppCompatActivity{
//局部变量
私有布尔snapBool=false;
私有布尔snapBool2=false;
私有布尔值snapBool3=false;
私有布尔值snapBool4=false;
私有布尔值snapBool5=false;
//为摄影机添加ImageView对象
私有图像视图快照;
私有ImageView snap2;
私有ImageView snap3;
私有ImageView snap4;
私有ImageView snap5;
专用静态最终int摄像机_请求=1888;
//添加用于服务器通信的单击事件的按钮
私人按钮发送;
//第1行
私有编辑文本idRow1;
私有编辑文本名row1;
私人纺纱机类别1;
私人编辑文本评论行1;
私人编辑文本价格Row1;
private ImageView galleryRow1;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//第1行
idRow1=(EditText)findViewById(R.id.idBox);
nameRow1=(EditText)findViewById(R.id.nameBox);
categoryRow1=(微调器)findViewById(R.id.cateBox);
commentRow1=(EditText)findViewById(R.id.commBox);
priceRow1=(EditText)findViewById(R.id.priceBox);
galleryRow1=(ImageView)findViewById(R.id.gallBox);
//摄影机事件的侦听器
snap=(ImageView)findViewById(R.id.gallBox);
snap.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent cameraIntent=newintent(android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
startActivityForResult(摄像机帐篷、摄像机请求);
//标记局部布尔值
snapBool=true;
}
});
snap2=(ImageView)findViewById(R.id.gallBox2);
snap2.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent cameraIntent=newintent(android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
startActivityForResult(摄像机帐篷、摄像机请求);
//标记局部布尔值
snapBool2=true;
}
});
snap3=(ImageView)findViewById(R.id.gallBox3);
snap3.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent cameraIntent=newintent(android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
startActivityForResult(摄像机帐篷、摄像机请求);
//标记局部布尔值
snapBool3=真;
}
});
snap4=(ImageView)findViewById(R.id.gallBox4);
snap4.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent cameraIntent=newintent(android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
startActivityForResult(摄像机帐篷、摄像机请求);
//标记局部布尔值
snapBool4=真;
}
});
snap5=(ImageView)findViewById(R.id.gallBox5);
snap5.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
Intent cameraIntent=newintent(android.provider.MediaStore.ACTION\u IMAGE\u CAPTURE);
startActivityForResult(摄像机帐篷、摄像机请求);
//标记局部布尔值
snapBool5=真;
}
});
//确认按钮将数据发送到服务器
sendBtn=(按钮)findviewbyd(R.id.send);
sendBtn.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
//创建AsyncTask以获取用户输入的单元格数据
新建MyTask().execute();
}
});
}//*******在创建端************
/*
*方法,该方法激活相机并获取相机的照片缩略图
*表单元格
*/
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
if(requestCode==CAMERA\u请求和&resultCode==RESULT\u确定){
位图照片=(位图)数据.getExtras().get(“数据”);
if(snapBool==true){
snap.setImageBitmap(照片);
}else if(snapBool2==true){
snap2.setImageBitmap(照片);
}else if(snapBool3==true){
snap3.setImageBitmap(照片);
}else if(snapBool4==true){
snap4.setImageBitmap(照片);
}else if(snapBool5==true){
snap5.setImageBitmap(照片);
}
//将布尔标志设置回false
snapBool=false;
snapBool2=false;
snapBool3=false;
snapBool4=假;
snapBool5=假;
}
}
/*
*方法获取第1行的单元格数据,并使用
*用php发布数据
*/
公共单元格(){
//在框中获取数据
字符串str1=idRow1.getText().toString();
字符串str2=nameRow1.getText().toString();
字符串str3=categoryRow1.getSelectedItem().toString();
字符串str4=commentRow1.getText().toString();
字符串str5=priceRow1.getText().toString();
//加列罗1.get
//创建sql对象
mysqlconnectsql=newmysqlconnect(str1、str2、str3、str4、str5,“png23.png”);
sql.sendTo();
}
/**
*异步任务类
*/
私有类MyTask扩展了AsyncTask{
@凌驾
公共关系
<?xml version="1.0" encoding="utf-8"?>
<!-- Header Table -->
<TableLayout
android:id="@+id/topTable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left|top"
android:background="#B3EAFF">
<TableRow android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:padding="10dip">
<TextView
android:id="@+id/idTitle"
android:text="id"
android:gravity="right"
android:paddingRight="70dp"/>
<TextView
android:text="Name"
android:paddingRight="75dp"/>
<TextView
android:text="Category"
android:paddingRight="55dp" />
<TextView
android:text="Comment"
android:paddingRight="45dp" />
<TextView
android:text="Price"
android:paddingRight="30dp" />
<TextView
android:text="Gallery" />
</TableRow>
<!-- drawn line -->
<View
android:layout_height="2dip"
android:background="#FF909090" />
</TableLayout>
<!-- Scroll Layout -->
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/scrollView"
android:layout_below="@+id/topTable"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:scrollbars="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:orientation="vertical"
android:id="@+id/linearLayout1"
android:layout_height="fill_parent"
android:layout_alignTop="@+id/scrollView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<!-- Row 1 -->
<EditText
android:layout_width="70dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:id="@+id/idBox" />
<EditText
android:layout_width="110dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:id="@+id/nameBox"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/idBox"
android:layout_toEndOf="@+id/idBox" />
<Spinner
android:layout_width="110dip"
android:layout_height="45dip"
android:entries="@array/cate_array"
android:prompt="@string/cate_spin"
android:background="@drawable/border_style"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/nameBox"
android:layout_toEndOf="@+id/nameBox"
android:id="@+id/cateBox" />
<EditText
android:layout_width="120dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/cateBox"
android:layout_toEndOf="@+id/cateBox"
android:id="@+id/commBox" />
<EditText
android:layout_width="60dip"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:digits="0123456789."
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/commBox"
android:layout_toEndOf="@+id/commBox"
android:id="@+id/priceBox" />
<!--<EditText
android:layout_width="90dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:onClick="true"
android:text=" [ O ]"
android:editable="false"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/priceBox"
android:layout_toEndOf="@+id/priceBox"
android:id="@+id/gallBox" />-->
<ImageView
android:layout_width="90dip"
android:layout_height="45dip"
android:background="@drawable/border_style"
android:onClick="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/priceBox"
android:layout_toEndOf="@+id/priceBox"
android:id="@+id/gallBox"/>
<!-- Row 2 -->
<EditText
android:layout_width="70dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_below="@+id/idBox"
android:id="@+id/idBox2" />
<EditText
android:layout_width="110dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:id="@+id/nameBox2"
android:layout_below="@+id/nameBox"
android:layout_toRightOf="@+id/idBox2" />
<Spinner
android:layout_width="110dip"
android:layout_height="45dip"
android:entries="@array/cate_array"
android:prompt="@string/cate_spin"
android:background="@drawable/border_style"
android:layout_toRightOf="@+id/nameBox2"
android:layout_below="@+id/cateBox"
android:id="@+id/cateBox2" />
<EditText
android:layout_width="120dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_toRightOf="@+id/cateBox2"
android:layout_below="@+id/commBox"
android:id="@+id/commBox2" />
<EditText
android:layout_width="60dip"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:digits="0123456789."
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_toRightOf="@+id/commBox2"
android:layout_below="@+id/priceBox"
android:id="@+id/priceBox2" />
<ImageView
android:layout_width="90dip"
android:layout_height="45dip"
android:background="@drawable/border_style"
android:onClick="true"
android:layout_toRightOf="@+id/priceBox2"
android:layout_below="@+id/gallBox"
android:id="@+id/gallBox2"/>
<!-- Row 3 -->
<EditText
android:layout_width="70dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_below="@+id/idBox2"
android:id="@+id/idBox3" />
<EditText
android:layout_width="110dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:id="@+id/nameBox3"
android:layout_below="@+id/nameBox2"
android:layout_toRightOf="@+id/idBox3" />
<Spinner
android:layout_width="110dip"
android:layout_height="45dip"
android:entries="@array/cate_array"
android:prompt="@string/cate_spin"
android:background="@drawable/border_style"
android:layout_toRightOf="@+id/nameBox3"
android:layout_below="@+id/cateBox2"
android:id="@+id/cateBox3" />
<EditText
android:layout_width="120dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_toRightOf="@+id/cateBox3"
android:layout_below="@+id/commBox2"
android:id="@+id/commBox3" />
<EditText
android:layout_width="60dip"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:digits="0123456789."
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_toRightOf="@+id/commBox3"
android:layout_below="@+id/priceBox2"
android:id="@+id/priceBox3" />
<ImageView
android:layout_width="90dip"
android:layout_height="45dip"
android:background="@drawable/border_style"
android:onClick="true"
android:layout_toRightOf="@+id/priceBox3"
android:layout_below="@+id/gallBox2"
android:id="@+id/gallBox3"/>
<!-- Row 4 -->
<EditText
android:layout_width="70dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_below="@+id/idBox3"
android:id="@+id/idBox4" />
<EditText
android:layout_width="110dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:id="@+id/nameBox4"
android:layout_below="@+id/nameBox3"
android:layout_toRightOf="@+id/idBox4" />
<Spinner
android:layout_width="110dip"
android:layout_height="45dip"
android:entries="@array/cate_array"
android:prompt="@string/cate_spin"
android:background="@drawable/border_style"
android:layout_toRightOf="@+id/nameBox4"
android:layout_below="@+id/cateBox3"
android:id="@+id/cateBox4" />
<EditText
android:layout_width="120dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_toRightOf="@+id/cateBox4"
android:layout_below="@+id/commBox3"
android:id="@+id/commBox4" />
<EditText
android:layout_width="60dip"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:digits="0123456789."
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_toRightOf="@+id/commBox4"
android:layout_below="@+id/priceBox3"
android:id="@+id/priceBox4" />
<ImageView
android:layout_width="90dip"
android:layout_height="45dip"
android:background="@drawable/border_style"
android:onClick="true"
android:layout_toRightOf="@+id/priceBox4"
android:layout_below="@+id/gallBox3"
android:id="@+id/gallBox4"/>
<!-- Row 5 -->
<EditText
android:layout_width="70dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_below="@+id/idBox4"
android:id="@+id/idBox5" />
<EditText
android:layout_width="110dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:id="@+id/nameBox5"
android:layout_below="@+id/nameBox4"
android:layout_toRightOf="@+id/idBox5" />
<Spinner
android:layout_width="110dip"
android:layout_height="45dip"
android:entries="@array/cate_array"
android:prompt="@string/cate_spin"
android:background="@drawable/border_style"
android:layout_toRightOf="@+id/nameBox5"
android:layout_below="@+id/cateBox4"
android:id="@+id/cateBox5" />
<EditText
android:layout_width="120dip"
android:layout_height="wrap_content"
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_toRightOf="@+id/cateBox5"
android:layout_below="@+id/commBox4"
android:id="@+id/commBox5" />
<EditText
android:layout_width="60dip"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:digits="0123456789."
android:background="@drawable/border_style"
android:singleLine="true"
android:scrollHorizontally="true"
android:layout_toRightOf="@+id/commBox5"
android:layout_below="@+id/priceBox4"
android:id="@+id/priceBox5" />
<ImageView
android:layout_width="90dip"
android:layout_height="45dip"
android:background="@drawable/border_style"
android:onClick="true"
android:layout_toRightOf="@+id/priceBox5"
android:layout_below="@+id/gallBox4"
android:id="@+id/gallBox5"/>
<!-- Send to server -->
<Button
android:id="@+id/send"
android:layout_width="80dp"
android:layout_height="40dp"
android:text="Send"
android:layout_below="@+id/idBox5"
android:layout_marginTop="15dp" />
</RelativeLayout>
</ScrollView>
<?php
$servername = "******";
$username = "*****";
$password = "*****";
$dbname = "*****";
// POST data
$idPass = $_POST['id'];
$namePass = $_POST['name'];
$catePass = $_POST['category'];
$commPass = $_POST['comment];
$pricePass = $_POST['price'];
//$gallPass = $_POST['gallery'];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO viewer VALUES ('$idPass', '$namePass', '$catePass', '$commPass', '$pricePass', 'imge.png')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
private class MyTask extends AsyncTask<String, Void, Void> {
public void sendTo() {
new Thread(new Runnable() {
@Override
public void run() {
// HTTP client
HttpClient client = new DefaultHttpClient();
// use php to post data to
HttpPost post = new HttpPost("http://myPHPfile.com/myFile.php");
try {
// add to arraylist
List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
// add values
nameValuePairs.add(new BasicNameValuePair("id", this.getId() ));
nameValuePairs.add(new BasicNameValuePair("name", this.getName()));
nameValuePairs.add(new BasicNameValuePair("category", this.getCate()));
nameValuePairs.add(new BasicNameValuePair("comment", this.getComm()));
nameValuePairs.add(new BasicNameValuePair("price", this.getPrice()));
//nameValuePairs.add(new BasicNameValuePair("gallery", this.getGall()));
// send to server - POST
post.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//HttpResponse response = client.execute(post);
client.execute(post);
} catch (ClientProtocolException ed) {
// TODO Auto-generated catch block
ed.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}