app正在关闭与本地xampp服务器的click-server连接

1hdlvixo  于 2021-06-24  发布在  Mysql
关注(0)|答案(1)|浏览(225)

我已经通过php和mysql做了很多。现在我将重点放在为使用xampp中的php服务器创建和设置的web应用程序创建android应用程序上。我的问题是最初的应用程序强制关闭由于一些运行时错误,这是由我的代码错误造成的。但现在的问题是,应用程序关闭时没有任何强制关闭通知。而且数据库也没有更新我通过应用程序输入的数据。
addentry.java文件:

package localhost80.sample1;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

import java.lang.String;

public class addEntry extends AppCompatActivity {

public EditText Eed, Elno, Epar, Epcs, Emts, Ewt, Ect, Egw, Efw, Erem;
public Spinner Evar;
public String str_ed, str_lno, str_par, str_pcs, str_mts, str_wt, str_ct, str_var, str_gw, str_fw, str_rem, str_var_code;
public Button Add, Clr;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_add_entry);
    addInput();
}

private void addInput() {
    Eed = findViewById(R.id.ed);
    Elno = findViewById(R.id.lno);
    Epar = findViewById(R.id.par);
    Epcs = findViewById(R.id.pcs);
    Emts = findViewById(R.id.mts);
    Ewt = findViewById(R.id.wt);
    Ect = findViewById(R.id.ct);
    Egw = findViewById(R.id.gw);
    Efw = findViewById(R.id.fw);
    Erem = findViewById(R.id.rem);
    Evar = findViewById(R.id.var);
    Add = findViewById(R.id.btnAdd);
    Clr = findViewById(R.id.btnClr);

    Add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            str_ed = (Eed.getText()).toString();
            str_lno = (Elno.getText()).toString();
            str_par = (Epar.getText()).toString();
            str_pcs = (Epcs.getText()).toString();
            str_mts = (Emts.getText()).toString();
            str_wt = (Ewt.getText()).toString();
            str_ct = (Ect.getText()).toString();
            str_gw = (Egw.getText()).toString();
            str_fw = (Efw.getText()).toString();
            str_rem = (Erem.getText()).toString();
            str_var = (Evar.getSelectedItem()).toString();

            switch (Evar.getSelectedItemPosition()) {
                case 0:
                    str_var_code = "fw";
                    break;
                case 1:
                    str_var_code = "brw";
                    break;
                case 2:
                    str_var_code = "mw";
                    break;
                case 3:
                    str_var_code = "bw";
                    break;
                case 4:
                    str_var_code = "ow";
                    break;
            }
            displayToast(str_ed, str_lno, str_par, str_pcs, str_mts, str_wt, str_ct, str_var, str_gw, str_fw, str_rem);
            serverLinking hostlink = new serverLinking(addEntry.this);
            hostlink.execute(str_ed,str_lno,str_par,str_pcs,str_mts,str_wt,str_ct,str_var_code,str_gw,str_fw,str_rem,"POST");
            finish();
         }
    });
    Clr.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Eed.setText("");
            Elno.setText("");
            Epar.setText("");
            Epcs.setText("");
            Emts.setText("");
            Ewt.setText("");
            Ect.setText("");
            Egw.setText("");
            Efw.setText("");
            Erem.setText("");
            Evar.setSelection(0);
        }
    });
}

private void displayToast(String d_ed, String d_lno, String d_par, String d_pcs, String d_mts, String d_wt, String d_ct, String d_var, String d_gw, String d_fw, String d_rem) {
    Toast toast = new Toast(getApplicationContext());
    String toasttext;
    toasttext = "Entry date : " + d_ed + "\nLot no : " + d_lno + "\nParty : " + d_par + "\nTotal Pcs : " + d_pcs + "\nTotal mtrs : " + d_mts + "\nTotal Weight : " + d_wt + "\nCount : " + d_ct + "\nVariety : " + d_var + "\nGray Width : " + d_gw + "\nFinishing Width : " + d_fw + "\nRemarks : " + d_rem;
    toast.setGravity(Gravity.TOP | Gravity.START, 0, 0);
    Toast.makeText(addEntry.this, toasttext, Toast.LENGTH_LONG).show();
}

}
服务器链接.java:

package localhost80.sample1;

    import android.content.Context;
    import android.os.AsyncTask;
    import android.widget.Toast;

    import org.apache.http.HttpResponse;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.methods.HttpGet;
    import org.apache.http.impl.client.DefaultHttpClient;

    import java.io.BufferedWriter;
    import java.io.IOException;
    import java.io.InputStream;
    import java.io.OutputStream;
    import java.io.OutputStreamWriter;
    import java.net.HttpURLConnection;
    import java.net.MalformedURLException;
    import java.net.URI;
    import java.net.URL;
    import java.net.URLEncoder;

    public class serverLinking extends AsyncTask<String, Void, String>
    {
private String link;
private Context context;
private String ed,lno,par,pcs,mts,wt,ct,var,gw,fw,remarks,method;

public serverLinking(Context context)
{
    this.context = context;
}

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

@Override
protected String doInBackground(String... arg0)
{
        method = arg0[11];
        link = "http://192.168.0.104/narmadaa_trial1/android/add.php";
        if(method.equals("POST"))
        {
            ed = arg0[0];
            lno = arg0[1];
            par = arg0[2];
            pcs = arg0[3];
            mts = arg0[4];
            wt = arg0[5];
            ct = arg0[6];
            var = arg0[7];
            gw = arg0[8];
            fw = arg0[9];
            remarks = arg0[10];
            try
            {
                URL url = new URL(link);
                HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
                httpURLConnection.setRequestMethod(method);
                httpURLConnection.setDoOutput(true);
                OutputStream os = httpURLConnection.getOutputStream();

                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));

                String data = URLEncoder.encode("ed","UTF-8")+"="+URLEncoder.encode("lno","UTF-8")+"="+URLEncoder.encode("par","UTF-8")+"="+
                        URLEncoder.encode("pcs","UTF-8")+"="+URLEncoder.encode("mts","UTF-8")+"="+URLEncoder.encode("wt","UTF-8")+"="+
                        URLEncoder.encode("ct","UTF-8")+"="+URLEncoder.encode("var","UTF-8")+"="+URLEncoder.encode("gw","UTF-8")+"="+
                        URLEncoder.encode("fw","UTF-8")+"="+URLEncoder.encode("remarks","UTF-8");

                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();

                InputStream IS = httpURLConnection.getInputStream();
                IS.close();
                return "Data Added Successfully";
            }
            catch (MalformedURLException e){
                e.printStackTrace();
            }
            catch (IOException e){
                e.printStackTrace();
            }
        }
    return null;
}

protected void onPostExecute(String result)
{
    Toast.makeText(context,result,Toast.LENGTH_LONG).show();
}

protected void onProgressUpdate(Void... values)
{
    super.onProgressUpdate(values);
}
    }

我仍然不知道我的代码出了什么问题。数据库在执行时既不更新。据我所知,php文件没有任何问题,我省略了本节中的php部分。
请任何人帮帮我。我被这个问题困住了。

3b6akqbq

3b6akqbq1#

您将finish()方法直接放在异步任务的开始之后,因为异步任务实际上启动了一个新线程,所以您的应用程序在serverlinking仍在工作时调用finish()方法。现在,我不知道android是否会自动处理这个问题,为了解决这个问题,可以尝试将finish()放在onpostexecute()中,如下所示:

protected void onPostExecute(String result) { 
    Toast.makeText(context,result,Toast.LENGH_LONG).show();
    ((Activity)context).finish();
}

把它从现在的地方移走。我不知道它是否会起作用,但这不是一个坏习惯

相关问题