Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将以下代码放入AsyncTask中_Php_Android_Android Asynctask - Fatal编程技术网

Php 如何将以下代码放入AsyncTask中

Php 如何将以下代码放入AsyncTask中,php,android,android-asynctask,Php,Android,Android Asynctask,此代码用于将值传递到php页面并更新数据库中的行 我需要以下代码进入asynctask,以及之后如何调用它等等。我得到了android.os.networkonmainthreadexception错误 public void dbUpdate(ArrayList<NameValuePair> data, String php) { InputStream iS = null; try { HttpClient httpcli

此代码用于将值传递到php页面并更新数据库中的行 我需要以下代码进入asynctask,以及之后如何调用它等等。我得到了android.os.networkonmainthreadexception错误

    public void dbUpdate(ArrayList<NameValuePair> data, String php)
    {
    InputStream iS = null;

    try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(php);
        httppost.setEntity(new UrlEncodedFormEntity(data));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        iS = entity.getContent();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in http connection " + e.toString());
        Toast.makeText(getBaseContext(), "Error " + e.toString(), Toast.LENGTH_LONG).show();
    }
}
公共void数据库更新(ArrayList数据,字符串php) { InputStream=null; 尝试 { HttpClient HttpClient=新的DefaultHttpClient(); HttpPost HttpPost=新的HttpPost(php); setEntity(新的UrlEncodedFormEntity(数据)); HttpResponse response=httpclient.execute(httppost); HttpEntity=response.getEntity(); iS=entity.getContent(); } 捕获(例外e) { e(“Log_标记”,“http连接错误”+e.toString()); Toast.makeText(getBaseContext(),“Error”+e.toString(),Toast.LENGTH\u LONG.show(); } } 这是目前我按钮中的代码

public void Update(View v)
{
    try
    { 

        String nM = this.Name.getText().toString();
        String tP = this.Type.getText().toString();
        String bR = this.Breed.getText().toString();
        String gE = this.Gender.getText().toString();
        String iN = this.Injuries.getText().toString();
        String tR = this.Treat.getText().toString();

        ArrayList<NameValuePair> up = new ArrayList<NameValuePair>();
        up.add(new BasicNameValuePair("name", nM));
        up.add(new BasicNameValuePair("type", tP));
        up.add(new BasicNameValuePair("breed", bR));
        up.add(new BasicNameValuePair("gender", gE));
        up.add(new BasicNameValuePair("injuries", iN));
        up.add(new BasicNameValuePair("treatment", tR));

        String php = "http://select.garethprice.co.za/update.php?nM=" + nM;

        dbUpdate(up, php);

        Toast.makeText(getBaseContext(), "Successfully updated", Toast.LENGTH_LONG).show();
    }
    catch(Exception e)
    {
        Log.e("log_tag", "Error in uploading " + e.toString());
        Toast.makeText(getBaseContext(), "Error " + e.toString(), Toast.LENGTH_LONG).show();
    }
}
公共作废更新(视图v)
{
尝试
{ 
字符串nM=this.Name.getText().toString();
字符串tP=this.Type.getText().toString();
String bR=this.Breed.getText().toString();
字符串gE=this.Gender.getText().toString();
String iN=this.injustions.getText().toString();
字符串tR=this.Treat.getText().toString();
ArrayList up=新的ArrayList();
添加(新的BasicNameValuePair(“名称”,nM));
添加(新的BasicNameValuePair(“类型”,tP));
添加(新的BasicNameValuePair(“品种”,bR));
添加(新的BasicNameValuePair(“性别”,gE));
向上添加(新的BasicNameValuePair(“伤害”),在中);
添加(新的BasicNameValuePair(“治疗”,tR));
字符串php=”http://select.garethprice.co.za/update.php?nM=“+nM;
dbUpdate(up,php);
Toast.makeText(getBaseContext(),“已成功更新”,Toast.LENGTH_LONG.show();
}
捕获(例外e)
{
Log.e(“Log_标记”,“上传错误”+e.toString());
Toast.makeText(getBaseContext(),“Error”+e.toString(),Toast.LENGTH\u LONG.show();
}
}
提前谢谢你

是一个关于异步任务的好教程。这些调用需要连接到后端并获取像这样的数据

HttpClient httpclient = new DefaultHttpClient();
    HttpPost httppost = new HttpPost(php);
    httppost.setEntity(new UrlEncodedFormEntity(data));
    HttpResponse response = httpclient.execute(httppost);
    HttpEntity entity = response.getEntity();
    iS = entity.getContent();

可以放在
doInBackground()
中,而结果的处理可以放在
postExecute()
方法中

它将类似于以下内容:

public class MyAsyncTask extends AsyncTask<Void, Void, Boolean> {

     protected void onPreExecute() {
     }

     protected Boolean doInBackground(Void param) {
        InputStream iS = null;

        try
        {
            HttpClient httpclient = new DefaultHttpClient();
            HttpPost httppost = new HttpPost(php);
            httppost.setEntity(new UrlEncodedFormEntity(data));
            HttpResponse response = httpclient.execute(httppost);
            HttpEntity entity = response.getEntity();
            iS = entity.getContent();
        }
        catch(Exception e)
        {
            Log.e("log_tag", "Error in http connection " + e.toString());
            return false;
        }

        return true;
     }

     protected void onPostExecute(Boolean result) {
        if(result)
            Toast.makeText(getBaseContext(), "Successfully updated", Toast.LENGTH_LONG).show();
         else
            Toast.makeText(getBaseContext(), "Error " + e.toString(), Toast.LENGTH_LONG).show();
     }
公共类MyAsyncTask扩展了AsyncTask{
受保护的void onPreExecute(){
}
受保护的布尔doInBackground(Void参数){
InputStream=null;
尝试
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(php);
setEntity(新的UrlEncodedFormEntity(数据));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
iS=entity.getContent();
}
捕获(例外e)
{
e(“Log_标记”,“http连接错误”+e.toString());
返回false;
}
返回true;
}
受保护的void onPostExecute(布尔结果){
如果(结果)
Toast.makeText(getBaseContext(),“已成功更新”,Toast.LENGTH_LONG.show();
其他的
Toast.makeText(getBaseContext(),“Error”+e.toString(),Toast.LENGTH\u LONG.show();
}
将在UI线程上调用
onPreExecute
onPostExecute
。这意味着可以创建对话框、祝酒词等

doInBackground
将在新的工作线程上运行。这将运行您的网络代码。
doInBackground
返回一个布尔值,以指示是否应显示toast

然后在按钮上运行它,如下所示:
newmyasynctask().execute();
替换此调用:
dbUpdate(up,php);


Android官方文档对此做了很好的解释:。

异步任务是一个可以在另一个类中调用的类,用于在后台线程中发出HTTP请求。您的AsyncTask类将被称为dbUpdate。当您希望执行AsyncTask中的操作时,请在Approver中包含以下行过时的方法或听众

new dbUpdate().execute();
这将在外部类中,现在我们需要为HTTP post请求配置AsyncTask类。创建一个
公共类dbUpdate extends AsyncTask
。现在这个类将有三个方法

@Override
public void onPreExecute() { super.onPreExecute(); }
protected String doInBackground(String... args) { /* more code here */ }
@Override
protected void onPostExecute(Boolean result) {
    super.onPostExecute(result);
    if(result) Toast.makeText(getBaseContext(), "Successfully updated", Toast.LENGTH_LONG).show();
}
现在,如果您希望在请求之前或之后运行任何代码,请在超级调用onPreExecuteonPostExecute中包含它们。在doInBackground中,将是您的HTTP请求(以下代码)。您可以将所有字符串设置为全局变量,以便可以在两个类中修改它们

String nM = this.Name.getText().toString();
String tP = this.Type.getText().toString();
String bR = this.Breed.getText().toString();
String gE = this.Gender.getText().toString();
String iN = this.Injuries.getText().toString(); 
String tR = this.Treat.getText().toString();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://select.garethprice.co.za/update.php?nM=" + nM);
try{
    List<NameValuePair> params = new ArrayList<NameValuePair>();
    params.add(new BasicNameValuePair("name", nM));
    params.add(new BasicNameValuePair("type", tP));
    params.add(new BasicNameValuePair("breed", bR));
    params.add(new BasicNameValuePair("genders", gE));
    params.add(new BasicNameValuePair("issues", iN));
    params.add(new BasicNameValuePair("treatment", tR));
    httppost.setEntity(new UrlEncodedFormEntity(params));
    httpclient.execute(httppost);
} catch (Exception e) {
    Log.e("log_tag", "Error in uploading " + e.toString());
    Toast.makeText(getBaseContext(), "Error " + e.toString(), Toast.LENGTH_LONG).show();
}
return null;
String nM=this.Name.getText().toString();
字符串tP=this.Type.getText().toString();
String bR=this.Breed.getText().toString();
字符串gE=this.Gender.getText().toString();
String iN=this.injustions.getText().toString();
字符串tR=this.Treat.getText().toString();
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://select.garethprice.co.za/update.php?nM=“+nM);
试一试{
List params=new ArrayList();
参数添加(新的BasicNameValuePair(“名称”,nM));
参数添加(新的BasicNameValuePair(“类型”,tP));
参数添加(新的BasicNameValuePair(“品种”,bR));
参数添加(新的BasicNameValuePair(“性别”,gE));
参数添加(新的BasicNameValuePair(“问题”),在中);
参数添加(新的BasicNameValuePair(“治疗”,tR));
setEntity(新的UrlEncodedFormEntity(参数));
httpclient.execute(httppost);
}捕获(例外e){
Log.e(“Log_标记”,“上传错误”+e.toString());
Toast.makeText(getBaseContext(),“Error”+e.toString(),Toast.LENGTH\u LONG.show();
}
返回null;
像这样。它是
private class YourAsyncTask extends AsyncTask<Void, Void, Boolean>
{    
     @Override
     protected Void doInBackground(Void... arg0)
    {
        try
        {
             String nM = this.Name.getText().toString();
             String tP = this.Type.getText().toString();
             String bR = this.Breed.getText().toString();
             String gE = this.Gender.getText().toString();
             String iN = this.Injuries.getText().toString(); 
             String tR = this.Treat.getText().toString(); 

             ArrayList<NameValuePair> up = new ArrayList<NameValuePair>();
             up.add(new BasicNameValuePair("name", nM)); up.add(new BasicNameValuePair("type", tP));
             up.add(new BasicNameValuePair("breed", bR));
             up.add(new BasicNameValuePair("gender", gE)); up.add(new BasicNameValuePair("injuries", iN));
             up.add(new BasicNameValuePair("treatment", tR)); 

             String php = "http://select.garethprice.co.za/update.php?nM=" + nM; dbUpdate(up, php); 

            dbUpdate(up, php);
        }
       catch(Exception e)
       {
           Log.e("log_tag", "Error in uploading " + e.toString());
           Toast.makeText(getBaseContext(), "Error " + e.toString(), Toast.LENGTH_LONG).show(); 
          return false;
       }

        return true;
    }

     @Override
     protected void onPostExecute(Boolean result)
    {
         if (result)
         {
               Toast.makeText(getBaseContext(), "Successfully updated", Toast.LENGTH_LONG).show();
         }
    }
}